2017-01-10 70 views
-1

我有一個圖像的base64字符串。將其轉換爲字節數組&,然後將其轉換爲memoryStream。我怎樣才能將這個圖像下載到客戶端機器。 base64字符串或圖像不保存在服務器/文件夾/任何位置。它是動態創建的。我試過無法將圖像下載到客戶端機器

1)Response.ContentType =「image/png」=>給出的異常obj未設置爲對象的瞬間。這樣

Context.Response.Clear(); 
Context.Response.ClearHeaders(); 
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + registrationId + ".png"); 
Context.Response.ContentType = "image/jpeg"; 
Response.AddHeader("Content-Length", bytes.Length.ToString()); 
//Response.ContentType = "application/octet-stream"; 
Context.Response.BinaryWrite(bytes); 

2)使用網絡客戶端=>給出異常

using (WebClient client = new WebClient()) 
{ 
    client.DownloadFile(ms1.ToString(), registrationId + ".png"); 
} 

任何其他溶液?

+0

[germankiwi's answer](http://stackoverflow.com/a/12631127/5588347)? –

+0

爲什麼使用Response.AddHeader(「Content-Length」,bytes.Length.ToString());'沒有'Context.'? – GraDea

+0

在第二種情況下,您只需嘗試將文件下載到服務器。它不起作用。 – GraDea

回答

0

只是試圖改變

Response.AddHeader("Content-Length", bytes.Length.ToString()); 

Context.Response.AddHeader("Content-Length", bytes.Length.ToString()); 

我想你沒有有效的Page對象在這種情況下。

+0

@GraDea ...出錯你有什麼建議..「價值不在預期的範圍內。」 –