2017-08-27 7 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 

を私はすべてを試みたが、それは22キロバイトである一方で、ファイルの唯一の800バイトがダウンロードされています。

ブラウザからのダウンロードは問題ありません。

助けてください!

ありがとうございます!

+2

実際のURLを投稿して試すことができますか? – Youssef13

+0

同じファイルを別のサーバーにアップロードしましたが、このコードは正常に動作しました。しかし、これは私が使用しなければならないサーバです。それを修正する方法はありますか? – MatrixCow08

+1

ブラウザでも質問コードのリンクが機能していません。 – Youssef13

答えて

0

問題は私のサーバーにあった、私はこのサーバからファイルをダウンロードすることはできません。 申し訳ありません@ Youssef13 - クッキーを使用しても機能しませんでした。ここで

は、ソリューションです: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

私のためにクッキーを使用して、あなたは例外や小さなファイルを取得しているかどうか説明できますか? – 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を参照してください。

はこれを使用します。

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

関連する問題