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:

  • Open Excel .
  • Click on File > Options.
  • Select Trust Center > Trust center settings.
  • Select Protected view.
  • Uncheck all the options under Protected View > OK.
  • Restart Excel and try to open Excel documents.
  • 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.

  • Browse to the folder using Windows Explorer
  • Right click on the Temp folder
  • Go to the Permissions tab
  • Grant the current user Full Control.
  • 链接地址: http://www.djcxy.com/p/46882.html

    上一篇: XLS / XLSX无法在使用APACHE POI创建的IPAD / Safari中打开

    下一篇: 下载的文件已损坏,而不是