FTP下载空白PDF vb.net

时间:2016-01-21 13:59:54

标签: vb.net pdf ftp downloading

所以我编写了代码,用于将pdf文件上传到FTP服务器上。一旦在服务器上,我可以通过Chrome从服务器下载它,它将正常工作。但我想通过我的程序下载它,所以我写了一些更多的代码,但是,我得到的文件的大小是0kb,所以是空的。我该如何解决这个问题?

Module Module1

Sub Main()
    Dim output As IO.Stream
    Dim FileName As String
    Console.WriteLine("Enter the name of the file to download:")
    FileName = Console.ReadLine
    Try
        Dim request As System.Net.FtpWebRequest
        request = DirectCast(System.Net.WebRequest.Create("ftp://ftp.drivehq.com/Folder/" & FileName & ".pdf"), System.Net.FtpWebRequest)
        'request.Method=Net.WebRequestMethods.Ftp.GetFileSize 
        'FileLength = cint(FTPRequest.getresponse.contentLength)
    Catch ex As Exception

    End Try

    Try
        Dim request As System.Net.FtpWebRequest
        request = DirectCast(System.Net.WebRequest.Create("ftp://ftp.server.com/Folder/" & FileName & ".pdf"), System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential("Username", "Password")
        request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
        Dim stream As System.IO.Stream = request.GetResponse.GetResponseStream
        'Dim OutPutFilepath As String = "DownloadTest" & "\" & IO.Path.GetFileName("ftp://ftp.Server.com/Folder/")
        output = System.IO.File.Create("C:\Users\ASUS\Documents\TestFile2.pdf")

        output.Close()
        stream.Close()
        Console.WriteLine("Downloaded")
        Console.ReadLine()


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

End Module

感谢。

1 个答案:

答案 0 :(得分:0)

确保将流中的数据写入文件:

    Dim stream As System.IO.Stream = request.GetResponse.GetResponseStream
    output = System.IO.File.Create("C:\Users\ASUS\Documents\TestFile2.pdf")
    stream.CopyTo(output)
    output.Close()
    stream.Close()