简体   繁体   English

从服务器 C# 下载时 Zip 文件已损坏

[英]Zip file is corrupted when downloaded from server C#

I have a zipped a file and while downloading it from the server using C# code it is showing invalid while extracting, I have tried extracting the file directly from server and it was working.我有一个压缩文件,在使用 C# 代码从服务器下载它时,它在提取时显示无效,我尝试直接从服务器提取文件并且它正在工作。

So, here the file seems to be corrupted only during download.因此,这里的文件似乎仅在下载过程中损坏。 This was working correctly a few days before but now it is showing issue.几天前这工作正常,但现在显示问题。

Can you please check my code and tell what could be the problem here, the same code works correctly in my local development environment.您能否检查我的代码并告诉这里可能存在什么问题,相同的代码在我的本地开发环境中可以正常工作。

Already tried,已经试过了,

1.Tried extracting the file directly in server and it worked , so issue must be with my downloading code. 1.尝试直接在服务器中提取文件并且它有效,所以问题一定是我的下载代码。

2.The same download code is working correctly. 2.相同的下载代码工作正常。

Note : File is already zipped in the server and the following code just downloads the zipped file, zipping is fine as file is opening in server注意:文件已经在服务器中压缩,下面的代码只是下载压缩文件,压缩是好的,因为文件在服务器中打开

protected void linkbtntempdownload_Click(object sender,EventArgs e)
            {
                string downloadlocation = string.Empty;
                if(ConfigurationManager.AppSettings["Environment"] == "prod")
                {
                    downloadlocation = ConfigurationManager.AppSettings["Templates"].ToString();
                }else
                {
                    downloadlocation = Server.MapPath("~/TemplateFolder/Templates.zip");
                }

                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "attachment; filename=" + "_Templates.Zip");

                Response.WriteFile(downloadlocation);
                Response.Flush();
                Response.SuppressContent = true;
                HttpContext.Current.ApplicationInstance.CompleteRequest();



            }

Hi was able to rectify the issue finally.嗨终于能够纠正这个问题。 The reason was that the zip file was corrupted.原因是 zip 文件已损坏。

when zip file is opened with notepad it should start with P. But when opened our file we found an additional string was appended to the file.当用记事本打开 zip 文件时,它应该以 P 开头。但是当打开我们的文件时,我们发现文件中附加了一个额外的字符串。 we were able to find that a Respose.write("string") was present in our server code that was appending as the additional string in the zip file.我们能够发现在我们的服务器代码中存在一个Respose.write("string")作为附加字符串附加到 zip 文件中。 After removing it the file opened properly.删除它后,文件正确打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM