0

我目前使用 FTPWebRequest 将所有内容上传到我的 Web 服务器。我在我的服务器上使用 ZFTP 并每年支付 1999.00 的许可证费用。ZFTP 有很多问题,所以我一直在尝试其他方法将图像上传到我的网络服务器。到目前为止,我还没有取得任何成功,相信我,这不是因为缺乏尝试。几乎所有我想做的就是将图像从我的计算机上传到我的家庭/Web 服务器,就像我对 FTPWebRequest 所做的那样,但想要使用 HTTPWebRequest 或类似的东西。以下是我的 4 次尝试,尽管我已经尝试了 MSDN 中的所有内容并将一些 C# 示例转换为 VB,但都没有运气。

Public Sub LoadImage()
        'Try1
        Dim client As New System.Net.WebClient
        Dim uriString As New System.Uri("http://XXXXXXXXXXXX/cart.png")
        client.UploadFileAsync(uriString, "C:\Users\dstrange\Pictures\add_to_cart.png")

        'Try 2
        Try
            My.Computer.Network.UploadFile("C:\Users\dstrange\Pictures\add_to_cart.png", "http://XXXXXXXXXXXX/img.png", "tyjacobs", "", True, 500)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        'Try 4
        My.Computer.Network.UploadFile("C:\Users\dstrange\Pictures\add_to_cart.png", "http://XXXXXXXXXXX/somefile.png", "tyjacobs", "")


        'Try 3
        Dim fileToUpload As String = "C:\Users\dstrange\Pictures\add_to_cart.png"
        Dim fileLength As Long = My.Computer.FileSystem.GetFileInfo(fileToUpload).Length

        Dim url As String = "http://XXXXXXXXXXX/img"
        Dim port As String = "443"

        If port <> "" Then

            Dim u As New Uri(url)

            Dim host As String = u.Host

            url = url.Replace(host, host & ":" & port)
            url = url.TrimEnd("/"c) & "/" & IO.Path.GetFileName(fileToUpload)

            Dim userName As String = "XXXXXXXXXXXX"
            Dim password As String = "XXXXXXXX"

            request = DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)

            request.Credentials = New NetworkCredential(userName, password)

            request.Method = WebRequestMethods.Http.Put

            request.ContentLength = fileLength

            request.SendChunked = True
            request.Headers.Add("Translate: f")
            request.AllowWriteStreamBuffering = True

            Dim s As IO.Stream = request.GetRequestStream()

            Dim fs As New IO.FileStream(fileToUpload, IO.FileMode.Open, _
                            IO.FileAccess.Read)

            Dim byteTransferRate As Integer = 1024
            Dim bytes(byteTransferRate - 1) As Byte
            Dim bytesRead As Integer = 0
            totalBytesRead = 0

            Do

                bytesRead = fs.Read(bytes, 0, bytes.Length)

                If bytesRead > 0 Then

                    totalBytesRead += bytesRead

                    s.Write(bytes, 0, bytesRead)

                End If

            Loop While bytesRead > 0

            s.Close()
            s.Dispose()
            s = Nothing

            'Close the file
            fs.Close()
            fs.Dispose()
            fs = Nothing


        End If
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim code As HttpStatusCode = response.StatusCode

        'Close the response
        response.Close()
        response = Nothing

        If totalBytesRead = fileLength AndAlso _
            code = HttpStatusCode.Created Then

            MessageBox.Show("The file has uploaded successfully!", "Upload Complete", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else

            MessageBox.Show("The file did not upload successfully.", _
                "Upload Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning)

        End If


    End Sub
4

1 回答 1

0

HTTP Post Files这是一篇涵盖基础知识的文章。

于 2013-07-20T02:38:51.383 回答