2016-03-28 5 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

@शेखरエラーはありません、例外はありません、私はデバッグを試みました – Stacky

答えて

0

あなたはTransmitFileを使用して正しい軌道に乗っている次のコード

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

申し訳ありませんが、私はあなたを取得していない? – Stacky

+0

私はこのようにしてみましたが、動作しません – Stacky

0

を使用してストリームを作成します。この行を確認してください:

Response.ContentType = ContentType; 

ContentTypeをページのデフォルトに設定しているようです。

明示的に指定してみてください:

Response.ContentType = "image/jpeg"; 
関連する問題