2016-03-28 50 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";