使用 WebClient 非同步上載檔案

2022-04-05 09:18:00 字數 1733 閱讀 5416

**比較簡單,但需要有幾點要注意的地方:

vb.net **

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

me.progressbar1.value = 0

if openfiledialog1.showdialog() = windows.forms.dialogresult.ok then

dim filename as string = openfiledialog1.filename

dim client as webclient = new webclient()

dim querystring as system.collections.specialized.namevaluecollection = new system.collections.specialized.namevaluecollection()

querystring.add("name", "mengxianhui")

querystring.add("from", "net_lover")

client.querystring = querystring

dim uri as uri = new uri("")

addhandler client.uploadfilecompleted, addressof uploadfilecompletedcallback

addhandler client.uploadprogresschanged, addressof uploadprogresscallback

client.uploadfileasync(uri, "post", filename)

end if

end sub

private sub uploadfilecompletedcallback(sender as object, e as uploadfilecompletedeventargs)

messagebox.show("上傳完畢")

end sub

private sub uploadprogresscallback(sender as object, e as uploadprogresschangedeventargs)

me.progressbar1.maximum = 100

me.progressbar1.minimum = 0

me.progressbar1.value = e.progresspercentage

end sub

接收檔案的**

upload.aspx **

對於大檔案上傳,需要在web.config檔案裡面設定最大的請求長度

web.config **

如果檔案在上傳到50%就立即完成了,則說明伺服器端可能出現了問題。請檢查檔案是否真的儲存到了伺服器。

c# **

private void button5_click(object sender, eventargs e) }

void client_uploadfilecompleted(object sender, uploadfilecompletedeventargs e)

void client_uploadprogresschanged(object sender, uploadprogresschangedeventargs e)

作者 孟憲會

利用WebClient類向伺服器上載檔案

net sdk 上面的開啟檔案的類 private filestream openfile else return null private void button2 click object sender,system.eventargs e private void button1 click ...

利用WebClient類向伺服器上載檔案

code net 提供了許多上載檔案的方法,在windows form應用程式中,我們可以使用webclient類來實現。webclient類也有兩個方法可以上載,uploadfile和openwrite方法,下面就是乙個實際的例子,兩種方法都有 結果如下 c 如下 using system usi...

在WebClient中使用post 傳送資料

webclient 類提供向 uri 標識的任何本地 intranet 或 internet 資源傳送資料以及從這些資源接收資料的公共方法。建構函式 ctor 包括 乙個空建構函式 和乙個靜態建構函式,靜態建構函式主要為urlencode 和urlencodeandwirte 編碼提供參照byte資...