Asp.net提供地址供下载文件

关键代码

protected void Page_Load(object sender, EventArgs e)
    {
        string action=Request.QueryString["action"];
        if (!String.IsNullOrEmpty(action))
        {
            switch (action)
            {
                case "app": DownloadFile("~/appfiles/BarCode.apk", "BarCode.apk");
                    break;
                case "version": DownloadFile("~/appfiles/Versions.xml", "Versions.xml");
                    break;
                default:
                    break;
            }
        }
        
        
    }
    private void DownloadFile(string _filepath, string _filename)
    {
        if (System.IO.File.Exists(MapPath(_filepath)))
        {
            Response.Clear();
            Response.Buffer = true;

            Response.AddHeader("Content-Disposition", "attachment;filename=" + _filename);
            Response.ContentType = "application/unknow";
            Response.TransmitFile(_filepath);
            Response.End();

        }
    }

测试地址

http://网站地址/appfiles/AppDownload.aspx?action=app
原文地址:https://www.cnblogs.com/hyyweb/p/7648934.html