传入的参数有:
`C:\Licenses\testfolder\PERSONAL-Wednesday 04 July-0405.txt`,`c2license.txt`它的功能是:
/// <summary>
/// Starts serving the download
/// </summary>
public static void InitStoreDownload(string filePath, string serveFileName)
{
// Get size of file
var f = new FileInfo(filePath);
var fileSize = f.Length;
var extension = f.Extension;
var context = HttpContext.Current;
context.Response.Clear();
context.Response.Buffer = false;
// Correct mime type
if (extension.Equals(".zip", StringComparison.CurrentCultureIgnoreCase))
context.Response.ContentType = "application/octet-stream";
else if (extension.Equals(".txt", StringComparison.CurrentCultureIgnoreCase))
{
context.Response.ContentType = "text/plain";
}
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + serveFileName);
context.Response.AddHeader("Content-Length", fileSize.ToString());
context.Response.TransmitFile(filePath);
context.Response.Close();
context.Response.End();
}服务器上的C:\Licenses\testfolder\PERSONAL-Wednesday 04 July-0405.txt文件的长度为475字节。
使用此脚本获取时下载的文件为474字节,文件末尾缺少一个字节。(最后一个字节是句号,存在于服务器上的文件中,但通过此函数下载时不存在)。这会导致文件变得无效。
我们正在挠头,试图找出为什么一个字节丢失了,有人能帮上忙吗?
发布于 2012-07-04 21:33:25
试着使用
Response.TransmitFile(filePath);
HttpContext.Current.ApplicationInstance.CompleteRequest(); 而不是
Response.Close();
Response.End();或者像其他人提到的那样:
在Close()之前调用Flush()
Response.TransmitFile(filePath);
Response.Flush();
Response.Close();
Response.End();或者省略对Close()的调用,直接调用End(),导致包含刷新响应。
Response.TransmitFile(filePath);
Response.End();这里有一个关于Response.End()的thread,也许它包含了对你有用的信息。
https://stackoverflow.com/questions/11330076
复制相似问题