asp.net 文件下载功能函数代码整理
发布时间:2020-12-15 20:43:41 所属栏目:asp.Net 来源:网络整理
导读:复制代码 代码如下: public void FileDownLoadDel(string fullFilename) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10000]; // Length of the file: int length; // Total bytes to read: long d
复制代码 代码如下: public void FileDownLoadDel(string fullFilename) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10000]; // Length of the file: int length; // Total bytes to read: long dataToRead; // Identify the file to download including its path. string filepath = fullFilename; filepath = Server.MapPath(filepath); // Identify the file name. string filename = System.IO.Path.GetFileName(filepath); try { // Open the file. iStream = new System.IO.FileStream(filepath,System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.Read); // Total bytes to read: dataToRead = iStream.Length; Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition","attachment; filename=" + filename); // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer,10000); // Write the data to the current output stream. Response.OutputStream.Write(buffer,length); // Flush the data to the HTML output. Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; Response.Clear(); } } Response.End(); //没有这句会将该页面刷新后的内容追加写入文件中。 } catch (Exception ex) { // Trap the error,if any. //Response.Write("Error : " + ex.Message); //base.WriteLog("资料","下载资料:" + ex.Message + "!",LogType.Error,this.GetType().ToString()); } finally { if (iStream != null) { //Close the file. iStream.Close(); } File.Delete(fullFilename); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 从经典ASP中的函数返回记录集
- asp.net – 从MSDN获取我需要的信息的最快方法是什么?
- asp.net – 如何在不依赖其扩展名的情况下检查上传文件的内
- Linq用另一个值替换null /空值
- ASP.NET中的Application和Cache有什么区别?
- asp.net c#的论坛软件,我可以整合到我的asp.net项目中吗?
- 如何在ASP.NET MVC中创建CheckBoxListFor扩展方法?
- asp.net-mvc-4 – ASP.NET MVC RemoteAttribute验证无效 –
- asp.net mvc 中 一种简单的 URL 重写
- asp.net – 在同一个域上的两个网站之间共享cookie
推荐文章
站长推荐
- asp.net – DirectoryServicesCOMException 8007
- iis-7 – ASP页面中的错误“80131509”
- asp.net – HttpClient不会从CookieContainer发送
- asp.net-mvc – 使用WebForm的MVC HtmlHelper
- asp.net-mvc – 如何将模型传递给部分视图
- ASP.Net下拉列表始终返回按钮单击事件的第一个值
- asp.net – 从代码后面添加文本框服务器控件?
- asp.net-mvc – 将项目从ASP.NET MVC1迁移到ASP.
- asp.net – 可以将OWIN Nancy与Web Forms一起使用
- asp.net – 测试Oracle存储过程的最简单的方法
热点阅读