Abo*_*our -3 c# asp.net download
我想给用户,链接到我在服务器上的文件,但我不想让他们直接链接到asp.net C#
例如 :
http://www.DomainName.com/Files/Downloads/setup.zip
转换成
创建一个页面,DownloadMgr.aspx 例如,从查询字符串中读取文件名并将其写入响应流.
就像是:
protected void Page_Load(object sender, EventArgs e)
{
string[] allowedExtensions = new string[] {".pdf",".zip",".txt", ".png"};
if (!this.Page.IsPostBack)
{
if (Request.QueryString["File"] != null)
{
if (Request.QueryString["File"].Contains("pdf"))
Response.ContentType = "application/pdf"; //varies depending on the file being streamed
Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"]);
if(allowedFiles.Contains(Path.GetExtension(Request.QueryString["File"])))
Response.WriteFile(Server.MapPath(Request.QueryString["File"]));
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您的所有链接都可以是: http://youhost.com/DownloadMgr.aspx?File=abc.pdf
| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |