Home ASP.Net MVC Razor Document Downloading as Corrupt
Reply: 0

ASP.Net MVC Razor Document Downloading as Corrupt

user1859
1#
user1859 Published in July 21, 2018, 9:53 am

I'm working on an ASP.Net project that is based off of Razor. We are posting documents from the client side using formdata and need to change the name of this file based on who is uploading it. We have a method for doing so, which you can see here:

var fileToSave = "";

if (filename != null) {
    fileToSave = '@ViewBag.CompName' + "-" + filename.name;
    fileToSave = fileToSave.replace(/\s+/g, '-');
}

var formData = new FormData();
formData.append("Document", filename, fileToSave);

This saves the document with the proper name, e.g. "Test-Company-test.docx"

The problem exists when I go to download the file and open it up. The document downloads properly, and has the appropriate size of the uploaded file, for this purpose, "test.docx".

When opening the downloaded file, Word prompts you with the following:

Pressing "OK" then shows the following message:

Following this message, if you click "Yes," the document opens and the entirety of the document is there. There is no corrupt data, and everything is formatted exactly how it is supposed to be.

This error does not exist if we just save the document as the original document name. Which normally we would do, but this is a large application that is client facing and allows users to upload documents themselves. This can be a problem if Company A uploads a document called "Test.docx" and Company B also uploads a document called "Test.docx" thus overwriting Company A's document. Hence, the renaming of the documents.

How is it that we can prevent this error from happening?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307357 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO