aspx.net 页面下文件下载功能函数整理

本文介绍了一个使用C#实现的文件下载及随后删除的方法。该方法通过读取指定路径下的文件,并将其以二进制流的形式发送给客户端,完成后从服务器上删除文件。涉及到的关键步骤包括打开文件流、读取数据并发送给客户端、处理客户端断开连接的情况等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 public void FileDownLoadDel(string fullFilename)
 2     {
 3         System.IO.Stream iStream = null;
 4         // Buffer to read 10K bytes in chunk:
 5         byte[] buffer = new Byte[10000];
 6 
 7         // Length of the file:
 8         int length;
 9 
10         // Total bytes to read:
11         long dataToRead;
12 
13         // Identify the file to download including its path.
14         string filepath = fullFilename;
15         filepath = Server.MapPath(filepath);
16 
17         // Identify the file name.
18         string filename = System.IO.Path.GetFileName(filepath);
19 
20 
21         try
22         {
23             // Open the file.
24             iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
25                         System.IO.FileAccess.Read, System.IO.FileShare.Read);
26 
27 
28             // Total bytes to read:
29             dataToRead = iStream.Length;
30 
31             Response.ContentType = "application/octet-stream";
32             Response.AddHeader("Content-Disposition""attachment; filename=" + filename);
33 
34             // Read the bytes.
35             while (dataToRead > 0)
36             {
37                 // Verify that the client is connected.
38                 if (Response.IsClientConnected)
39                 {
40                     // Read the data in buffer.
41                     length = iStream.Read(buffer, 010000);
42 
43                     // Write the data to the current output stream.
44                     Response.OutputStream.Write(buffer, 0, length);
45 
46                     // Flush the data to the HTML output.
47                     Response.Flush();
48 
49                     buffer = new Byte[10000];
50                     dataToRead = dataToRead - length;
51                 }
52                 else
53                 {
54                     //prevent infinite loop if user disconnects
55                     dataToRead = -1;
56                     Response.Clear();
57                 }
58             }
59             Response.End(); //没有这句会将该页面刷新后的内容追加写入文件中。
60         }
61         catch (Exception ex)
62         {
63             // Trap the error, if any.
64             //Response.Write("Error : " + ex.Message);
65             //base.WriteLog("资料", "下载资料:" + ex.Message + "!", LogType.Error, this.GetType().ToString());
66 
67         }
68         finally
69         {
70             if (iStream != null)
71             {
72                 //Close the file.
73                 iStream.Close();
74             }
75             File.Delete(fullFilename);
76         }
77     }

转载于:https://www.cnblogs.com/skyshenwei/archive/2010/04/06/1705026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值