2016-03-28 42 views
0

我正在使用此代碼來下載圖像,即我將數據庫中的圖像擴展名存儲在此處。爲什麼asp.net下載不工作?

此代碼不會引發錯誤,但不會下載任何內容。爲什麼?

try { 
    if (e.CommandName == "Download") 
    { 
     string ImgPath = Convert.ToString(r["Attachment"]); 
     //string ImgName = r["ComplaintDetailID"].ToString() + "-" + r["LetterNo"].ToString() + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"); 

     //string filePath = ImgName + ".jpg"; 
     string fullFilePath = Server.MapPath("~/SiteImages/CMS/" + ImgPath); 
     Response.Clear(); 
     Response.ClearHeaders(); 
     Response.ClearContent(); 
     Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fullFilePath)); 
     Response.ContentType = ContentType; 
     Response.TransmitFile(fullFilePath); //downloads file 
     Response.Flush(); 

     //FileInfo file = new FileInfo(fullFilePath); 
     //file.Delete(); //deletes file after downloading 
    } 
} 
catch (Exception ex) 
{ 
    ResultLabel.ResultLabelAttributes(ex.Message, ProjectUserControls.Enums.ResultLabel_Color.Red); 
} 
finally 
{ 
    ResultPanel.Controls.Add(ResultLabel); 
} 

更新:

我試過,但不起作用。

if (e.CommandName == "Download") 
      { 
       string ImgPath = Convert.ToString(r["Attachment"]); 
       //string ImgName = r["ComplaintDetailID"].ToString() + "-" + r["LetterNo"].ToString() + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"); 


         //string filePath = ImgName + ".jpg"; 
         MemoryStream m = new MemoryStream(); 
         File.OpenRead(ImgPath).CopyTo(m); 
         m.WriteTo(Response.OutputStream); 

         string fullFilePath = Server.MapPath("~/SiteImages/CMS/" + ImgPath); 
         Response.Clear(); 
         Response.ClearHeaders(); 
         Response.ClearContent(); 
         Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fullFilePath)); 
         Response.ContentType = ContentType; 
         Response.TransmitFile(fullFilePath); //downloads file 
         Response.Flush(); 

         //FileInfo file = new FileInfo(fullFilePath); 
         //file.Delete(); //deletes file after downloading 
      } 
+0

什麼錯誤? –

+0

@शेखरno error,no exception,we tried debugging – Stacky

回答

0

創建使用下面的代碼

MemoryStream m = new MemoryStream(); 
File.OpenRead(ImgPath).CopyTo(m); 
m.WriteTo(Response.OutputStream); 
+0

對不起,我沒有得到你? – Stacky

+0

我試過像這個BTU不工作 – Stacky

0

你使用TransmitFile在正確的軌道上的流。檢查這一行:

Response.ContentType = ContentType; 

它看起來像你將ContentType設置爲頁面的默認值。

嘗試顯式指定它:

Response.ContentType = "image/jpeg";