2013-07-19 58 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來做到這一點。 –