感谢您阅读我的帖子。我写了一个HttpResponse代码来下载文件。遵循代码:
string fullPath = Path.Combine(appPath, randomFileName);
File.WriteAllBytes(fullPath, data);
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.BufferOutput = true;
response.Cache.SetCacheability(HttpCacheability.Private);
response.CacheControl = "private";
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
response.AddHeader("Content-Length", data.Length.ToString((IFormatProvider)CultureInfo.InvariantCulture));
response.Flush();
response.BinaryWrite(data);
response.Flush();
response.End();
这里,File.WriteAllBytes
方法在服务器中创建一个文件,这是一个xlsx文件。在notepad ++中打开此文件后,该文件包含1506791个字符。在notepad ++中打开下载的xlsx文件时,该文件有1490407个字符。
下载的文件无法在Microsoft Excel中打开。我使用notepad ++来查看是否所有字节都已下载。显然,不会下载所有字节。
HttpResponse没有下载所有字节。
data.Length
方法是否可能不计算unicode字符,并且response.BinaryWrite
执行时binarywrite
会在到达data.Length
值时执行并停止字节的结尾。
答案 0 :(得分:0)
此方法在将相同数据发送回客户端之前使用您保存的文件(让我知道它是怎么回事):
string fullPath = Path.Combine(appPath, randomFileName);
File.WriteAllBytes(fullPath, data);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.ClearHeaders();
response.AddHeader("Content-Disposition", "inline; filename=" + randomFileName);
response.ContentType = "binary/octet-stream";
response.WriteFile(fullPath);