怎样实现下载文件完毕后删除文件?

cailee 2009-07-31 06:17:07
        string name = "123.xls";       
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";
Response.WriteFile(name);
Response.End();
System.IO.File.Delete(Server.MapPath(name));

最后一句代码不执行。

请问各位有什么好的方法么??
谢谢大家了。
...全文
261 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
peecars 2010-06-12
  • 打赏
  • 举报
回复
强人还是很多的吗!收益,受益
超维电脑科技 2009-08-01
  • 打赏
  • 举报
回复
关注
zhaoqiliang527 2009-07-31
  • 打赏
  • 举报
回复
学习...
cailee 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 insus 的回复:]
参考:

http://www.cnblogs.com/insus/articles/1431986.html
当天的,也许删除不了,那就做一个定时器,每天删除昨天或以前的.
[/Quote]
感谢。不过这个也太麻烦了。我只需要用户下载一次以后就删除。
cailee 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ihandler 的回复:]
以二进制流读取文件,读完后,输出、删除
是否需要控制用户取消下载的情形?
[/Quote]
不用。只需要一次下载。
cailee 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 angiexing 的回复:]
将文件读取到二进制文件流中,然后删除!之后再下载
//  获取模版文件
            if (!File.Exists(FileAllPath))
            {
                //如果下载的模板不存在
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "下载", "alert('该文件不存在!');", true);
                return;
            }
            FileStream fsManuscript = new FileStream(FileAllPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            int rgfilesize = Convert.ToInt32(fsManuscript.Length);
            byte[] byteArrManuscriptContent = new byte[rgfilesize];
            fsManuscript.Read(byteArrManuscriptContent, 0, rgfilesize);
            fsManuscript.Close();
            fsManuscript.Dispose();
//删除文件
if(File.Exits(fileAllPath)
File.Delete(fileAllPaath);
            // 下载文件
            downloadFileName = "a.xls";
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(downloadFileName, System.Text.Encoding.UTF8) + ";");
            Response.BinaryWrite(byteArrManuscriptContent);
            Response.End();
[/Quote]
可行。非常感谢。
和我之前想的一样,想写入内存再删除文件,再下载,可惜苦于无法下手。
待会给分。
杨哥儿 2009-07-31
  • 打赏
  • 举报
回复

string name = "123.xls";
System.IO.File.Delete(Server.MapPath(name));
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";
Response.WriteFile(name);
Response.End();

cailee 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 angiexing 的回复:]
if(File.Exist(Path))
{
  File.Delete(Path);
}
[/Quote]
就算加了判断文件是否存在,一样不会删除。
cailee 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cpp2017 的回复:]
Response.End();
        System.IO.File.Delete(Server.MapPath(name));   

=======>


        System.IO.File.Delete(Server.MapPath(name));
Response.End();


Response.End()了,后面的代码不会再执行.

   
[/Quote]
文件是删除了。但是无法下载了。
angiexing 2009-07-31
  • 打赏
  • 举报
回复
将文件读取到二进制文件流中,然后删除!之后再下载
// 获取模版文件
if (!File.Exists(FileAllPath))
{
//如果下载的模板不存在
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "下载", "alert('该文件不存在!');", true);
return;
}
FileStream fsManuscript = new FileStream(FileAllPath, FileMode.Open, FileAccess.Read, FileShare.Read);
int rgfilesize = Convert.ToInt32(fsManuscript.Length);
byte[] byteArrManuscriptContent = new byte[rgfilesize];
fsManuscript.Read(byteArrManuscriptContent, 0, rgfilesize);
fsManuscript.Close();
fsManuscript.Dispose();
//删除文件
if(File.Exits(fileAllPath)
File.Delete(fileAllPaath);
// 下载文件
downloadFileName = "a.xls";
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(downloadFileName, System.Text.Encoding.UTF8) + ";");
Response.BinaryWrite(byteArrManuscriptContent);
Response.End();
slimboy123 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 angiexing 的回复:]
if(File.Exist(Path))
{
  File.Delete(Path);
}
[/Quote]
up
IHandler 2009-07-31
  • 打赏
  • 举报
回复
以二进制流读取文件,读完后,输出、删除
是否需要控制用户取消下载的情形?
insus 2009-07-31
  • 打赏
  • 举报
回复
参考:

http://www.cnblogs.com/insus/articles/1431986.html
当天的,也许删除不了,那就做一个定时器,每天删除昨天或以前的.
angiexing 2009-07-31
  • 打赏
  • 举报
回复
if(File.Exist(Path))
{
File.Delete(Path);
}
cpp2017 2009-07-31
  • 打赏
  • 举报
回复
Response.End();
System.IO.File.Delete(Server.MapPath(name));

=======>


System.IO.File.Delete(Server.MapPath(name));
Response.End();


Response.End()了,后面的代码不会再执行.

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧