溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用webClient實(shí)現(xiàn)圖片同步,異步下載

發(fā)布時(shí)間:2020-05-31 17:54:30 來源:網(wǎng)絡(luò) 閱讀:652 作者:程沐喆 欄目:編程語言

WebClient.DownloadFile 方法

將具有指定 URI 的資源下載到本地文件。

命名空間:System.Net

程序集:System(在 system.dll 中)


同步實(shí)現(xiàn)參考代碼:

private void btnsyndownload_Click(object sender, EventArgs e)
{
File.Delete("download.jpg");
try
{
if (!File.Exists("download.jpg"))
{
WebClient client = new WebClient(); client.DownloadFile("https://ss0.baidu.com/Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=181318733ad12f2ed105a8607fc0d5ff/94cad1c8a786c9171cc12b9dc53d70cf3ac75757.jpg", "download.jpg");
picpicture.Image = Image.FromFile("download.jpg");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

    }

WebClient.DownloadFileAsync 方法

將具有指定 URI 的資源下載到本地文件。此方法不會(huì)阻止調(diào)用線程。

命名空間:System.Net
程序集:System(在 system.dll 中)


異步實(shí)現(xiàn)參考代碼:

File.Delete("download.jpg");
try
{
if (!File.Exists("download.jpg"))
{
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

client.DownloadFileAsync(newUri("https://ss0.baidu.com/Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=181318733ad12f2ed105a8607fc0d5ff/94cad1c8a786c9171cc12b9dc53d70cf3ac75757.jpg"),"download.jpg");

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
    void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {           
        picpicture.Image = Image.FromFile("download.jpg"); 
    }

簡(jiǎn)單介紹一下WebClient:

WebClient 類提供向 URI 標(biāo)識(shí)的任何本地、Intranet 或 Internet 資源發(fā)送數(shù)據(jù)以及從這些資源接收數(shù)據(jù)的公共方法。
WebClient 類使用 WebRequest 類提供對(duì)資源的訪問。WebClient 實(shí)例可以通過任何已向 WebRequest.RegisterPrefix 方法注冊(cè)的 WebRequest 子代訪問數(shù)據(jù)。
注意
默認(rèn)情況下,.NET Framework 支持以 http:、https:、ftp:、和 file: 方案標(biāo)識(shí)符開頭的 URI。 

下面描述用于將數(shù)據(jù)上載到資源的 WebClient 方法: 
OpenWrite  檢索一個(gè)用于將數(shù)據(jù)發(fā)送到資源的 Stream。
OpenWriteAsync  檢索 Stream,它在不阻止調(diào)用線程的情況下將數(shù)據(jù)發(fā)送到資源。
UploadData  將字節(jié)數(shù)組發(fā)送到資源,并返回包含任何響應(yīng)的 Byte 數(shù)組。
UploadDataAsync  在不阻止調(diào)用線程的情況下,將 Byte 數(shù)組發(fā)送到資源。
UploadFile  將本地文件發(fā)送到資源,并返回包含任何響應(yīng)的 Byte 數(shù)組。
UploadFileAsync  在不阻止調(diào)用線程的情況下,將本地文件發(fā)送到資源。
UploadValues  將 NameValueCollection 發(fā)送到資源,并返回包含任何響應(yīng)的 Byte 數(shù)組。
UploadValuesAsync  在不阻止調(diào)用線程的情況下,將 NameValueCollection 發(fā)送到資源,并返回包含任何響應(yīng)的 Byte 數(shù)組。
UploadString  在不阻止調(diào)用線程的情況下,將 String 發(fā)送到資源。
UploadStringAsync  在不阻止調(diào)用線程的情況下,將 String 發(fā)送到資源。 

下面描述從資源下載數(shù)據(jù)的 WebClient 方法: 
OpenRead  從資源以 Stream 的形式返回?cái)?shù)據(jù)。
OpenReadAsync  在不阻止調(diào)用線程的情況下,從資源返回?cái)?shù)據(jù)。
DownloadData 從資源下載數(shù)據(jù)并返回 Byte 數(shù)組。
DownloadDataAsync  在不阻止調(diào)用線程的情況下,從資源下載數(shù)據(jù)并返回 Byte 數(shù)組。
DownloadFile  從資源將數(shù)據(jù)下載到本地文件。
DownloadFileAsync  在不阻止調(diào)用線程的情況下,將數(shù)據(jù)從資源下載到本地文件。
DownloadString  從資源下載 String 并返回 String。
DownloadStringAsync  在不阻止調(diào)用線程的情況下,從資源下載 String。 
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI