2017-01-30 62 views
1

目前,我的表單元素看起來像如下:如何通過xhrPost()發送一個csv文件?

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

但不是在<form>給予actionenctypemethod,我想用dojo.xhrPost()來發送。

有人請告訴我如何使用xhrPost發送?

而且,我的休息這段代碼看起來象下面這樣:

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


    }); 
} 
+0

(// meta.stackoverflow.com/q/251361) – Tushar

+0

感謝編輯@Tushar – PPH

+0

你可能會發現這個[這](HTTP [我如何格式化我的代碼塊?]://計算器。 com/questions/14459618/multipart-form-data-ajax-request-in-dojo)有幫助 – Dheeresh

回答

0

在xhrPost和@Path註釋指定的路徑的URL是不一樣的。

您應該將一個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; 
     } 


    }); 
} 
+0

非常感謝。有效。 :)但是,我用'dojo.io.iframe.send({' – PPH

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