第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

C#如何通過(guò)URL下載圖片?

C#如何通過(guò)URL下載圖片?

陪伴而非守候 2019-04-13 08:45:19
生成圖片的URL假設(shè)是這樣:http://localhost/administrator/qrcode.aspx?pid=78qrcode.aspx.cs的生成圖片的部分代碼:Imageimage=newBitmap(200,200);Graphicsg=Graphics.FromImage(image);try{stringurl="http://localhost";DotNetBarcodebc=newDotNetBarcode();bc.Type=DotNetBarcode.Types.QRCode;bc.PrintCheckDigitChar=true;bc.WriteBar(url,0,0,210,210,g);System.IO.MemoryStreamms=newSystem.IO.MemoryStream();image.Save(ms,System.Drawing.Imaging.ImageFormat.Png);Response.ClearContent();Response.ContentType="image/Png";Response.BinaryWrite(ms.ToArray());ms.Dispose();}finally{g.Dispose();image.Dispose();}現(xiàn)在如果我要輸入http://localhost/administrator/qrcode.aspx?pid=78&download=true就下載圖片到本地要怎么做?找過(guò)一些資料,WebClient.DownloadFile方法不知道怎么用。滿足參數(shù)download=true的時(shí)候使用Image.Save(stringfilename,ImageFormatformat);似乎不起作用,不知道什么原因。
查看完整描述

2 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

已經(jīng)解決。
其實(shí)主要是讓瀏覽器直接下載文件而不是打開(kāi)文件。
要做兩步。
一就是把響應(yīng)頭的類型設(shè)置成application/octet-stream。
二就是設(shè)置HTTP響應(yīng)頭的名字為Content-Disposition,設(shè)定值為attachment;filename=theFilename。這里的theFileName就是文件下載對(duì)話框里面的默認(rèn)文件名。
Response.ClearContent();
Response.ContentType="application/octet-stream";
Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode("qrcode.png",System.Text.Encoding.UTF8));
Response.BinaryWrite(ms.ToArray());
                            
查看完整回答
反對(duì) 回復(fù) 2019-04-13
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

publicvoidDoGetImage(stringurl,stringpath)
{
HttpWebRequestreq=(HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue=false;
req.Method="GET";
req.KeepAlive=true;
req.ContentType="image/png";
HttpWebResponsersp=(HttpWebResponse)req.GetResponse();
System.IO.Streamstream=null;
try
{
//以字符流的方式讀取HTTP響應(yīng)
stream=rsp.GetResponseStream();
Image.FromStream(stream).Save(path);
}
finally
{
//釋放資源
if(stream!=null)stream.Close();
if(rsp!=null)rsp.Close();
}
}
                            
查看完整回答
反對(duì) 回復(fù) 2019-04-13
  • 2 回答
  • 0 關(guān)注
  • 476 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)