1

I am making an get request from server xyzserver/Education.doc and logging the response, it is showing that :

��ࡱ�>�� \^����[���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������` ��bjbj�s�s 4>�� �������2z#z#z#z#�#�2�@�6$6$6$6$6$I%I%I%r@t@t@t@t@t@t@$�AhED:�@ 'E%I% ' '�@6$6$�@�'�'�' '�6$6$r@�' 'r@�'�'�h9�<=6$*$��3���z#�'.<�=��@0�@,

I want to upload this data/file to another server through an XHR request.
I did this :

xhr.send({ url : "http://abcserver/abc.doc", method : "GET" }, function(data) {
  if ( data.status == 200 && data.response != null ) {
    var fd = new FormData();
    fd.append('file', data.response);
    XHR.open('POST', 'http://abc/upload');
    XHR.setRequestHeader('Content-Type', 'multipart/form-data');
    XHR.sendAsBinary(fd);
  }
  else { // something went wrong
    console.log("something is wrong");
  }
});

But it fails.
Here is the request header:

Accept:/ Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8,hi;q=0.6 Cache-Control:max-age=0 Connection:keep-alive Content-Length:59256 Content-Type:multipart/form-data;boundary=gc0p4Jq0M2Yt08jU534c0p Host:localhost:8080 Origin:http://stackoverflow.com Referer:Can't append <script> element User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36 Request Payload ------WebKitFormBoundaryDwpm9n3Ucp31JnIC Content-Disposition: form-data; name="file"

��ࡱ�>�� \^����[���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������` ��bjbj�s�s 4>�� �������2z#z#z#z#�#�2�@�6$6$6$6$6$I%I%I%r@t@t@t@t@t@t@$�AhED:�@ 'E%I% ' '�@6$6$�@�'�'�' '�6$6$r@�' 'r@�'�'�h9�<=6$*$��3���z#�'.<�=��@0�@,

What's happening? How can I solve this issue and achieve my upload?

Community
  • 1
  • 1
  • 1. try to send as post request 2. Encode it in base64 – Kristiyan Aug 03 '15 at 15:42
  • [**`THIS`**](http://snook.ca/archives/javascript/copying_from_mi) might help. – Shrinivas Shukla Aug 03 '15 at 15:44
  • Please look at my edit and make sure to make a good, readable post next time Jagdeep Singh. – Kyll Aug 03 '15 at 15:55
  • I don't want to open/show in browser. I just want to upload doc/pdf response to another server. I what format should i send this data through POST and what to write in java controller at back end. In back-end i am using Java. Please Suggest. – Jagdeep Singh Aug 04 '15 at 13:11

1 Answers1

0

Have a look on the MDN doc.

You may try to use

XHR.responseType = "arraybuffer";

to notify the browser you're working with some binary types.

Sample JSFiddle to fetch a .doc file

nioKi
  • 1,259
  • 9
  • 17
  • I am getting response like ........ ����������` ��bjbj�s� and while sending to another server it 's through an error of "file type not found " I am doing handleFileUpload(@RequestParam("file") MultipartFile doc){ String name= "jaggu"; if (!doc.isEmpty()) { try { byte[] bytes = doc.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name))); stream.write(bytes); stream.close(); return "Success"; and it is throwing error – Jagdeep Singh Aug 04 '15 at 08:32
  • On my browser (FF 41), when using the JSFiddle I linked, I don't have weird characters, but 'ArrayBuffer { byteLength: 5528 }'. Are you using an especially old brower ? I have no way to test the .doc upload right now, the only advice I can give you is to stick to the MDN doc and read it carefully. Also, you may try to set a Plunker or a JSFiddle up to allow us to help you in a better way. – nioKi Aug 04 '15 at 08:42
  • When i provide responseType = "arraybuffer" then null response is coming. http://jsfiddle.net/b32qt3ut/1/ if i comment "responseType = "arraybuffer" then response is coming and printing unknown many characters like "��bjbj�s�" – Jagdeep Singh Aug 04 '15 at 13:38
  • I can't even try, because of wrong CORS headers that prevent me from doing the AJAX request..I don't know why, but it won't work with me this time. Maybe you're doing tests from the same server ? ?.Sorry, you may need to find another test file. – nioKi Aug 04 '15 at 13:45