asp.net調用瀏覽器右鍵菜單“另存爲”

asp.net調用網頁右鍵另存爲圖片方法,這樣可以點擊圖片的時候默認是下載而不是簡單的打開圖片功能。

網頁根目錄新建一個DownloadFile.aspx頁面,下面代碼直接放去DownloadFile.aspx前臺頁面。其他頁面需要用到下面功能,超鏈接中使用:   DownloadFile.aspx?file=要下載的圖片Url

 

<%@ Page Language="C#" %>

<%
    //使用方法
    //DownloadFile.aspx?file=要下載的圖片url

    string url = HttpContext.Current.Request.Url.Query.ToLower();

    //file=dddd.jpg
    url = url.Replace("?file=", "");

    Response.BufferOutput = false;

    Response.Clear();
    Response.ContentType = "application/x-msdownload";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + url);
    Response.ContentType = "application/octstream";
    Response.CacheControl = "Private";
    System.IO.Stream stm = new System.IO.FileStream(Server.MapPath(url), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
    Response.AppendHeader("Content-length", stm.Length.ToString());

    System.IO.BinaryReader br = new System.IO.BinaryReader(stm);

    byte[] bytes;

    for (Int64 x = 0; x < (br.BaseStream.Length / 4096 + 1); x++)
    {
        bytes = br.ReadBytes(4096);
        Response.BinaryWrite(bytes);
        System.Threading.Thread.Sleep(5);  //休息一下,防止耗用帶寬太多。
    }

    stm.Close();

%>


 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章