檔案下載的製作

來源:互聯網
上載者:User

 很多時候我們去做檔案上傳,做完上傳覺得檔案下載變得非常簡單,

但很多時候下載會出亂碼名稱等一系列問題,自己做了個下載,把各個類中的源碼放一起了

可能有點亂,各位包含,其實主要是為了我自己以後用的方便,放到這裡

下載方法:

string filePath = thesisInfo.ThesisUrl.ToString().Trim();   //擷取檔案名稱
string Path = "";
if (Convert.ToInt32(Request["TYPE"]) == 1)
...{            //返回伺服器上的實體路徑,UpLoadURL是擷取WEBCONFIG裡配置的一個路徑,是下面的那個靜態方法
        Path = Server.MapPath(UpLoadFile.UpLoadURL + "ThesisFiles/") + filePath;
}
if (System.IO.File.Exists(Path))  //判斷路徑是否存在
...{
        string temp = string.Format("attachment;filename={0}", Server.UrlEncode(filePath));
        Response.ClearHeaders();
        Response.AppendHeader("Content-disposition", temp);

        Response.WriteFile(HttpUtility.UrlDecode(Path, System.Text.Encoding.UTF8));//解析檔案名稱防止亂碼
        Response.End();
}
else
...{
        Response.Write("該檔案不存在!");
}

 

 

 public static String UpLoadURL
        ...{
            get
            ...{                    //擷取WEBCONFIG上配置的哪個路徑,可以自己寫
                string stemp = ConfigurationManager.AppSettings[SystemConst.KEY_UpLoad];
                if (stemp == null)
                ...{
                    return SystemConst.DEFAULT_UpLoad;
                }
                else
                ...{
                    return stemp;
                }
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.