【发布时间】:2010-02-01 23:59:21
【问题描述】:
好的,我正在从服务器下载文件,我计划在客户端下载文件后删除在服务器上下载的文件..
我的下载代码工作正常,但我不知道何时输入删除文件的命令。
string filepath = restoredFilename.ToString();
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
//Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
//// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
}
现在我知道 response.End() 将停止一切并返回值,所以还有其他方法吗..
我需要调用一个函数
DeleteRestoredFileForGUI(restoredFilename);
删除文件但不知道放在哪里..我尝试在 Response.End() 之前和之后放置但它不起作用..
感谢任何帮助...谢谢
【问题讨论】:
-
运行此页面代码是否有效。而不是做 response.end() 运行 response.redirect("~/page-for-delete.aspx?file=delete.dat", false); - 通过添加 false 您继续处理此页面上的信息。
-
@Kieran,TransmitFile 将在文件传输时终止响应,而 Response.Redirect 将导致响应冲突。
标签: c# asp.net download response