我想通过 http 请求参数从 asp.net 网页提供动态创建的二进制文件,如下所示www.host.com/?Download=file
:到目前为止我有
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params.Allkeys.Contains("Download"))
{
String fileStr = Request.Params.GetValues("Download")[0];
using (Stream generatedFile = File.Create(@"C:\Temp\file"))
{
/* write the contents of the file */
}
Response.ContentType = "application";
Response.AddHeader("Content-Disposition",
"attachment; filename=" + fileStr);
Response.WriteFile(@"C:\Temp\file");
Response.End();
}
}
哪个有效,但 C:\Temp\file 仍然存在,我想清理它。调用File.Delete(@"C:\Temp\file");
afterResponse.End()
不起作用,因为会Response.End()
杀死线程。将Response方法放在using
块File.Create( (...), FileOptions.DeleteOnClose)
中不起作用,因为Response方法随后无法访问该文件。
有什么建议么?