dojo 如何通过xhrPost()发送csv文件?

rslzwgfq  于 2022-12-08  发布在  Dojo
关注(0)|答案(2)|浏览(160)

目前,我的表单元素如下所示:

<form enctype="multipart/form-data" name="copyReplaceForm" method="POST" action="/app/applications/copyreplace/postCsv">

但是我想用dojo.xhrPost()发送它,而不是在<form>上给出actionenctypemethod
有人能告诉我如何使用xhrPost发送吗?
此外,我的REST代码片段如下所示:

@POST
@Path("/bulkCopyReplaceFirst")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)

我的xhrPost如下所示

var result;
dojo.xhrPost({
        url :"/CopyReplace/bulkCopyR",
        preventCache: true,
        contentType : "multipart/form-data",
        load: function(response) {
                txtResponse = response;
                console.log("response is : txtResponse"+txtResponse)
        },
        error: function(error, ioArgs) {
                console.log("postImageOptions() ERROR :: " + error);
                console.log("postImageOptions() ioArgs :: " + ioArgs);
                return error;
        }

    });
}
biswetbf

biswetbf1#

xhrPost中的url与@Path注解中指定的路径不同。
您应该将form属性添加到xhrPost。

var result;
dojo.xhrPost({
        url :"/bulkCopyReplaceFirst",
        form: document.forms["copyReplaceForm"],
        load: function(response) {
                txtResponse = response;
                console.log("response is : txtResponse"+txtResponse)
        },
        error: function(error, ioArgs) {
                console.log("postImageOptions() ERROR :: " + error);
                console.log("postImageOptions() ioArgs :: " + ioArgs);
                return error;
        }

    });
}
j1dl9f46

j1dl9f462#

您可以直接使用Dojo Uploader。

var up = new Uploader({
            label: "Upload csv",
            multiple: false,       // true if you can upload more files
            uploadOnSelect: false,   // true if you want to upload without clicking on the submit of the from
            url: "/path/name.csv",   // the route path to the backend (xhr url)
            style: "",
            onBegin: function() {
               // start of upload
            },
            onProgress: function(rev) {
               // uploading...
            },
            onChange: function() {
               // on file change
               var file = up.getFileList();
             }
         }, this.domNode);

相关问题