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

时间:2017-01-30 03:39:54

标签: javascript html rest dojo dojo.xhrget

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

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

但我没有在action上提供enctypemethod<form>,而是希望使用dojo.xhrPost()发送。{/ p>

有人可以告诉我如何使用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;
        }


    });
}

2 个答案:

答案 0 :(得分:0)

xhrPost中的url和@Path注释中指定的路径不一样。

您应该向xhrPost添加form属性。

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;
        }


    });
}

答案 1 :(得分:0)

您可以直接使用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);