2017-08-27 97 views
0

我到處搜索但沒有任何好處。使用VB.net下載文件不完整

我使用這個代碼來下載文件:

Imports System.IO 
Imports System.Security.AccessControl 
Imports System.Net 
Imports System.ComponentModel 

Public Class Step4 

    Function GetUserName() As String 
     If TypeOf My.User.CurrentPrincipal Is 
      System.Security.Principal.WindowsPrincipal Then 
      ' The application is using Windows authentication. 
      ' The name format is DOMAIN\USERNAME. 
      Dim parts() As String = Split(My.User.Name, "\") 
      Dim username As String = parts(1) 
      Return username 
     Else 
      ' The application is using custom authentication. 
      Return My.User.Name 
     End If 
    End Function 

    Private Sub Step4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     ' Code of Step3 
     Dim URL As String = "http://skinsserver.exampleserver.com/skins.zip" 
     Dim filename As String = "C:\Users\" + GetUserName() + "\AppData\Roaming\Applicationsettings\skins.zip" 
     Using wc As New WebClient 
      wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)") 
      wc.DownloadFile(URL, filename) 
     End Using 
    End Sub 
End Class 

我什麼都試過,但只有800文件的字節被下載而它是22 KB。

通過瀏覽器下載是好的。

請幫忙!

謝謝!

+2

你可以發佈真實的網址,我們可以試試嗎? – Youssef13

+0

我上傳了另一臺服務器上的相同文件,此代碼工作正常。但是,這是我必須使用的服務器,以任何方式解決這個問題? – MatrixCow08

+1

即使在瀏覽器中,問題代碼中的鏈接也不起作用。 – Youssef13

回答

0

的問題是在我的服務器,我不能從該服務器上下載文件。 對不起@優素福13 - 即使使用cookie也不起作用。

這裏是解決方案:http://vbcity.com/blogs/xtab/archive/2016/04/13/how-to-upload-and-download-files-with-ftp-from-a-vb-net-application.aspx

我使用FTP下載文件。

+0

使用Cookie爲我工作,你能解釋,如果你得到異常或小文件? – Youssef13

+0

我得到相同的文件大小(800字節)。 – MatrixCow08

+0

我剛剛嘗試過這個代碼,它對我來說完美無缺。你能否將下載文件的擴展名更改爲txt並上傳其內容? – Youssef13

0

你可以嘗試以下方法:

My.Computer.Network.DownloadFile(
"http://skinsserver.exampleserver.com/skins.zip", 
"path_where_you_want_to_download") 

看到https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/computer-resources/how-to-download-a-file

+0

已經嘗試過了。一樣。 – MatrixCow08

+0

我上傳了另一臺服務器上的相同文件,此代碼工作正常。但是,這是我必須使用的服務器,以任何方式解決這個問題? – MatrixCow08

+0

上述兩種方法都應該可以工作並完成工作。可能是您的服務器配置有問題。 –

1

你想從下載的URL需要一個cookie。

使用此:

wc.Headers.Add("Cookie", "__test=3c020c18923605cf39e4292d69038f3a") 
+0

這工作!但我用另一種方法從FTP服務器下載:http://vbcity.com/blogs/xtab/archive/2016/04/13/how-to-upload-and-download-files-with-ftp-from -a-vb-net-application.aspx - 不,我將使用HTTP下載。 :) – MatrixCow08

+0

很高興聽到它爲你工作:) – Youssef13