簡體   English   中英

從 url 下載圖像

[英]Download of image from url

我必須使用 for 循環從特定的 URL 下載圖像,以防某些產品的圖像超過 5 個。當我單擊產品按鈕時,一些圖像僅下載而不是所有圖像。但是當我調試相應的代碼時工作正常,即它下載所有圖像..正確。 這是由於一些緩存......還是圖像文件是 Gif 格式......

這是我的代碼

for (int i = 0; i < obj.Count; i++)
{
PartNAme = (obj[i].ToString().Split('='))[1];
_prtnm = PartNAme.ToString().Split(';')[0];

//this is url , source for the image
_final_URI = _URI + _Prod_name + '/' + _prtnm + ".GIF";


WebClient client = new WebClient();

string strtempname = DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "");

rnd = new Random(100);

string _strfile = "PNImage" + strtempname + rnd.Next().ToString() + ".gif";

string _path = "../Images/PNImage/" + _strfile;

string _PPath = Server.MapPath(_path);

//to download image from source url, and path to save the image file
client.DownloadFile(_final_URI, _PPath);
}

在上面的代碼中,當我調試圖像文件時,圖像文件被正確下載,但是在沒有調試的情況下運行時,一些圖像文件被重復,所以不是獲取原始圖像文件,而是下載舊/相同的圖像文件......這是由於一些緩存。 ...或圖像文件為 Gif 格式....

我認為隨機給你相同的數字。 請參閱此線程以獲取僅生成一個隨機數的修復隨機數生成器

如果您進行調試,則無需調試就會“變慢”。 另一種選擇是使用guids作為文件名

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM