关于asp.net简单的下载问题

时间:2023-12-29 00:02:56

关于asp.net的下载,只需将打开相应的文件路径就能在浏览器上实现下载功能,比如项目的同级目录上有一个文件 苍老师.zip<a href="苍老师.zip"></a>

点击a标签就能下载这个文件。

但是今天用asp.net做一个下载碰到一个问题,找不到服务器上的web路径,但是从服务器上知道了文件的绝对路径  

于是新建了一个页面上传到服务器,点击下载跳转到该页面,在页面上获取到本地文件就能进行下载

页面代码如下:

    string url = "文件名.doc"
string DownloadTitle = "标题";
string downpath = "E:/Tiku_Soft/" + url;
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = downpath.Trim();
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + DownloadTitle + ".doc" + "\"");
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName.Trim());
HttpContext.Current.ApplicationInstance.CompleteRequest();