有些文件是从我的站点文件夹下载的。 我创建了一个CFilestream类,允许我下载大文件。
下载文件时,下载已正确完成,但在打开WORD文件时,会显示一条消息,指示文件已损坏。
我找不到任何解决方案。
拜托,你能帮帮我吗? 感谢
public class CFilestream : System.Web.UI.Page
{
public void WriteFileToResponse(string strSecureFilePath, string strUserFilename, string strContentType = @"application/octet-stream")
{
byte[] dataBlock = new byte[0x1000];
long fileSize;
int bytesRead;
long totalBytesRead = 0;
try
{
using (var fs = new FileStream(strSecureFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read))
{
fileSize = fs.Length;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = strContentType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + strUserFilename.Replace(" ", "_"));
Response.AddHeader("Content-Length", fileSize.ToString());
bool bContinue = true;
while (totalBytesRead < fileSize && bContinue)
{
if (!Response.IsClientConnected)
{
bContinue = false;
}
bytesRead = fs.Read(dataBlock, 0, dataBlock.Length);
Response.OutputStream.Write(dataBlock, 0, bytesRead);
Response.Flush();
totalBytesRead += bytesRead;
}
//Response.Close(); si activé problème dans chrome, téléchargement ne se termine pas
//Response.End();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}