Downloaded file is corrupt which is not
I am trying to download a file through webapi, which works perfectly, except for Excel. The excel file is downloaded successfully, and when I open the same it gives me a "File is corrupt" error. However, it is not corrupt.
When I search on the Internet it is asking me to follow these:
After changing this setting, I can open the downloaded file without any problem.
I can understand it is client side thing but I don't want to do the same on every single machine. Because, With my understanding on protection is that if you download any file, it will still open but it will be in a protected mode.
But in my case it is showing as "File is Corrupt", which is a wrong explanation for end-user.
I am sure there are many website out there where you can download from and it work as expected ie you can still open but in a protected mode.
Please note: File is uploaded from Excel 2013 and viewed from Excel 2010.
Here is the download code that I am currently using:
private HttpResponseMessage Execute()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
StreamContent streamContent = null;
if (Content == null)
{
streamContent = new StreamContent(File.OpenRead(MapPath(LocalPath)));
}
else
{
streamContent = new StreamContent(new MemoryStream(Content));
}
response.Content = streamContent;
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = DownloadFileName,
};
return response;
}
The content type is set as: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
I had the same problem with Protected Mode today with files downloaded from the Internet. It turned out that the underlying cause was that the user had lost their permissions to %TEMP% (normally c:usersusernameAppDataLocalTemp). MS Office puts the file in a sandbox in that folder to help protect you.
You can verify the location of %TEMP% by running
echo %TEMP%
on the command line for that user.
上一篇: XLS / XLSX无法在使用APACHE POI创建的IPAD / Safari中打开
下一篇: 下载的文件已损坏,而不是