2

While uploading doc file(example test.doc) to server(unix machine), I am using apache commons jar which gives me FormFile instance at server side which is having all the data in byte array form.

When I write the same byte array to response output stream and send it to browser to download the same file, weird content is shown. I get one pop up to select encoding in which i would like to see the data and weird data is shown in that doc.The content type is set as follows :

response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=test.doc");

I think that while writing data to output stream, meta data related to doc file is also written which causes this issue.

Is there anything specific for doc or docx file formats, which needs to be done so file is in proper format and i can see correct data which i uploaded or I am missing something?

Any help would be appreciated.

Thanks in Advance.

Let me know if more info is required.

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
  • 1
    Files are files; there's no manipulation done on the file content. It's more likely you have an issue with your content settings when you're downloading it. – Dave Newton Jun 09 '17 at 14:37
  • How are you reading the input stream when uploading? how are you writing the output stream when downloading? – Michele Mariotti Jun 20 '17 at 15:01
  • I found the solution for this. There was nothing wrong in reading and writing the data to file. Instead the issue was with one of the filter which was messing with the response. I really appreciate all the responses received so far. Thanks everyone. – RockAndRoll Jun 21 '17 at 06:11

2 Answers2

0

There's a known issue in Microsoft which provide workaround for the Encoding Pop Up

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

It may not be a fix for your problem because I have not run any test around. But to check the correct mime types please refer to this link:

https://technet.microsoft.com/en-us/library/ee309278(office.12).aspx

Updated:

You can use response type as ArrayBuffer and set the content as Blob.

Blob([response], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'});

Or this could work

response.setContentType("application/x-msdownload"); response.setHeader("Content-disposition", "attachment; filename="+ fileName);

Sudhesh Rajan
  • 367
  • 1
  • 7