2013-07-19 29 views
0

我目前使用FTPWebRequest将所有内容上传到我的Web服务器。我在我的服务器上使用ZFTP,每年支付1999.00美元的许可证。 ZFTP非常麻烦,所以我一直在尝试其他方法上传图像到我的网络服务器。到目前为止,我还没有取得任何成功并相信我,这不是因为缺乏尝试。几乎所有我想要做的就是从我的电脑上传图像到我的家庭/ Web服务器,就像我使用FTPWebRequest一样,但想要使用HTTPWebRequest或类似的东西。下面是我的4个尝试,虽然我已经尝试了MSDN中的所有内容,并将一些C#示例转换为VB,但都没有运气。将图像上传到首页/ Web服务器VB.net

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 

回答

0

HTTP Post Files这是一篇介绍基础知识的文章。

+0

这必须构建到应用程序中。我知道filezilla服务器和客户端是免费的,但这不是解决我的问题。 –

+0

已更新的答案。 – OneFineDay

+0

我已经使用FTPWebRequest进行上传。我不想做FTP上传。我需要使用HttpWebrequest来做到这一点。 –