關于asp.net的下載,只需將打開相應的文件路徑就能在瀏覽器上實現下載功能,比如項目的同級目錄上有一個文件 蒼老師.zip<a href="蒼老師.zip"></a>
點擊a標簽就能下載這個文件。
但是今天用asp.net做一個下載碰到一個問題,找不到服務器上的web路徑,但是從服務器上知道了文件的絕對路徑
于是新建了一個頁面上傳到服務器,點擊下載跳轉到該頁面,在頁面上獲取到本地文件就能進行下載
頁面代碼如下:
1 1 string url = "文件名.doc" 2 2 string DownloadTitle = "標題"; 3 3 string downpath = "E:/Tiku_Soft/" + url; 4 4 HttpContext.Current.Response.ContentType = "application/ms-download"; 5 5 string s_path = downpath.Trim(); 6 6 System.IO.FileInfo file = new System.IO.FileInfo(s_path); 7 7 HttpContext.Current.Response.Clear(); 8 8 HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream"); 9 9 HttpContext.Current.Response.Charset = "utf-8";10 10 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=/"" + DownloadTitle + ".doc" + "/"");11 11 HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());12 12 HttpContext.Current.Response.WriteFile(file.FullName.Trim());13 13 HttpContext.Current.ApplicationInstance.CompleteRequest();
新聞熱點
疑難解答