通用处理程序文件下载未开始
Generic handler file download does not start
我正在尝试开始从服务器下载文件,现在只有一些存在文件的硬编码值,但由于某种原因下载没有开始,也没有抛出任何错误。
这是我的代码:
public void ProcessRequest(HttpContext context)
{
string destPath = context.Server.MapPath("~/Attachments/cover.txt");
// Check to see if file exist
FileInfo fi = new FileInfo(destPath);
if (fi.Exists)
{
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "cover.txt");
HttpContext.Current.Response.BinaryWrite(ReadByteArryFromFile(destPath));
HttpContext.Current.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
private byte[] ReadByteArryFromFile(string destPath)
{
byte[] buff = null;
FileStream fs = new FileStream(destPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(destPath).Length;
buff = br.ReadBytes((int)numBytes);
return buff;
}
我正在执行代码,没有出现任何问题,但浏览器中也没有显示文件下载弹出窗口。
你看有什么不对吗?
我认为您遇到的问题是您的电话 HttpContext.Current
。由于您使用的是 通用处理程序文件 ,我相信您会希望使用传递给您的方法签名的 context
参数。一个例子是:
public void ProcessRequest (HttpContext context)
{
// Build Document and Zip:
BuildAndZipDocument();
// Context:
context.Response.ContentType = "application/zip";
context.Response.AddHeader("content-disposition", "filename="Commodity.zip");
zip.Save(context.Response.OutputStream);
// Close:
context.Response.End();
}
我相信如果您使用 context
而不是 HttpContext.Current
,它将解决您的问题。
切勿在 Content-Type header 中使用 "application/zip" 作为 ZIP 文件。绝不!已确认的错误存在于 IE6 中,这将破坏下载。
如果您希望二进制文件在过去和现在的大多数浏览器中具有最通用的行为,请始终使用 "application/octet-stream",就像您在其他地方看到的一样!
header('Content-Type: application/octet-stream');
如果您关心的话,这会克服 IE6 错误。尽管如此,从 application/octet-stream 切换到 application/zip 不会有任何收获,因此您不妨停止在那个上面浪费时间并保持它 application/octet-stream.
我正在尝试开始从服务器下载文件,现在只有一些存在文件的硬编码值,但由于某种原因下载没有开始,也没有抛出任何错误。
这是我的代码:
public void ProcessRequest(HttpContext context)
{
string destPath = context.Server.MapPath("~/Attachments/cover.txt");
// Check to see if file exist
FileInfo fi = new FileInfo(destPath);
if (fi.Exists)
{
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "cover.txt");
HttpContext.Current.Response.BinaryWrite(ReadByteArryFromFile(destPath));
HttpContext.Current.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
private byte[] ReadByteArryFromFile(string destPath)
{
byte[] buff = null;
FileStream fs = new FileStream(destPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(destPath).Length;
buff = br.ReadBytes((int)numBytes);
return buff;
}
我正在执行代码,没有出现任何问题,但浏览器中也没有显示文件下载弹出窗口。
你看有什么不对吗?
我认为您遇到的问题是您的电话 HttpContext.Current
。由于您使用的是 通用处理程序文件 ,我相信您会希望使用传递给您的方法签名的 context
参数。一个例子是:
public void ProcessRequest (HttpContext context)
{
// Build Document and Zip:
BuildAndZipDocument();
// Context:
context.Response.ContentType = "application/zip";
context.Response.AddHeader("content-disposition", "filename="Commodity.zip");
zip.Save(context.Response.OutputStream);
// Close:
context.Response.End();
}
我相信如果您使用 context
而不是 HttpContext.Current
,它将解决您的问题。
切勿在 Content-Type header 中使用 "application/zip" 作为 ZIP 文件。绝不!已确认的错误存在于 IE6 中,这将破坏下载。
如果您希望二进制文件在过去和现在的大多数浏览器中具有最通用的行为,请始终使用 "application/octet-stream",就像您在其他地方看到的一样!
header('Content-Type: application/octet-stream');
如果您关心的话,这会克服 IE6 错误。尽管如此,从 application/octet-stream 切换到 application/zip 不会有任何收获,因此您不妨停止在那个上面浪费时间并保持它 application/octet-stream.