2013-02-15 73 views
0

有沒有辦法下載用戶上傳的圖片?我有以下幾點:首先,用戶點擊馬賽克中的圖像來詳細查看;然後圖像在asp:image標籤中生成,在該步驟中,它們應該通過單擊按鈕來下載它。我遇到的問題是圖像是在asp:image標記中生成的,而不是具有名稱的實際圖像本身。我必須下載的代碼才能將圖像存儲在文件夾中。下面是代碼:如何用C#中的按鈕點擊下載圖片?

protected void btnDownload_Click(object sender, EventArgs e) 
{ 
    string path = @"C:\inetpub\wwwroot\PSCSearchEngine\MemberPages\Images\live.jpg";   
    System.IO.FileInfo file = new System.IO.FileInfo(path); 
    if (file.Exists) 
    { 
     Response.Clear(); 
     Response.ContentType = MimeType(Path.GetExtension(path)); 
     Response.AddHeader("Content-Disposition", 
      string.Format("attachment; filename = {0}", 
      System.IO.Path.GetFileName(path))); 
     Response.AddHeader("Content-Length", file.Length.ToString("F0")); 
     Response.TransmitFile(path); 
     Response.End(); 
    } 
    else 
    { 
     Response.Write("This file does not exist."); 
    } 
} 

下面是預覽圖像的代碼:

@"~/MemberPages/UpdatePhoto.aspx?SiteKey=" + foo.Site_ID 
    + "&TimeStamp=" + foo.timestamp[n1 - 1]; 
+0

您可以在服務器上使用某些緩存以保留具有會話級別標識的映像。 – Romoku 2013-02-15 17:00:31

+0

那麼,它必須是動態的,用戶可以返回馬賽克並下載其他圖像。謝謝。 – Jacman 2013-02-15 17:25:49

+0

然後在您的數據庫中創建一個表格,以存儲用戶選擇哪個圖像以及哪些圖像可供用戶使用。 – Romoku 2013-02-15 17:41:51

回答

0

代碼下載使用按鈕的圖像文件,單擊C#

protected void btnDownload_Click(object sender, EventArgs e) 
{ 
    string filename=MapPath("birds.jpg"); 
    Response.ContentType = "image/JPEG"; 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename+ ""); 

    Response.TransmitFile(filename); 
    Response.End(); 
} 
0

使用Web客戶端

using System; 
using System.Net; 

// ... 
    WebClient webClient = new WebClient(); 
    webClient.DownloadFile("http://orig14.deviantart.net/d7a5/f/2016/123/0/9/judy_and_nick_by_kuvshinov_ilya-da15pnt.jpg", "MyFile.jpg"); 
0

您可以試試這個:

using (var client = new System.Net.WebClient()) 
{ 
    var _imagebytes = client.DownloadData(string.Format(@"~/MemberPages/UpdatePhoto.aspx?SiteKey={0}&TimeStamp={1}", foo.Site_ID, foo.timestamp[n1 - 1])); 
    Response.Clear(); 
    Response.ContentType = "image/jpg"; 
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", "MyImage.jpg")); 
    Response.AddHeader("Content-Length", _imagebytes.Length.ToString("F0")); 
    Response.OutputStream.Write(_imagebytes, 0, _imagebytes.Length); 
    Response.End(); 
}