2013-11-15 78 views
0

我想發送文件到使用VB.net的服務器。我發現很多例子都說它很容易做,但我發現的例子都沒有發揮作用。VB.net上傳文件

目前一個我想在下面的代碼:

Dim WithEvents wc As New System.Net.WebClient() 
Private Sub oWord_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles oWord.DocumentBeforeClose 
    Try 

     Using wc As New System.Net.WebClient() 
      wc.Credentials = New NetworkCredential("ehavermale", "ernie1") 
      wc.UploadFile("http://192.168.95.1:83/GraphTest.txt", "C:\Users\EHovermale\Desktop\GraphTest.txt") 
     End Using 
    Catch ex As Exception 
     MsgBox("Error:" + ex.Message) 
    End Try 

    'System.IO.File.Delete("C:\Users\EHovermale\Desktop\GraphTest.txt") 

    MsgBox("See Ya") 
End Sub 

當我運行這個程序,我得到的錯誤:Web客戶端請求時發生異常。

我有機會讀/寫文件到服務器,我試圖打。

有另一種方式來上傳文件或者是有毛病我對這樣的代碼?

謝謝!

+2

我可以看到的最有可能的事情,不知道這臺服務器上是什麼,是你發佈到錯誤的「服務」。由於沒有指定路徑,因此您的文件將被傳送到Web服務器的「Default Index」,可能是default.aspx或index.html。如果Web服務器未配置爲接受此地址的文件帖子,則不會發生任何事情。你可能需要指定一個像? wc.UploadFile( 「HTTP://本地主機:52234/FileReceivedHandler.ashx」, 「C:\用戶\ davidr \桌面\ foo.txt的」) – laylarenee

+0

你是 「控制」 這個服務器? (「http://192.168.95.1:83」) – laylarenee

+0

我改變了路徑,爲它添加一個文件名,仍然得到相同的錯誤。我也在控制這臺服務器。我可能會誤解這個過程是如何工作的,我正在尋找一個函數來發送一個文件從我的電腦存儲在我們的服務器上,這是正確的方法來實現這個@DavidR – Ehaver282

回答

-1

由於沒有HTTP服務處理文件上傳,你可以直接保存使用VBA的Scripting.FileSystemObject的文件。如果您可以從您的文檔所在的位置訪問網絡共享,這將起作用。請記住,如果文檔被移到另一臺計算機上,那麼這可能不起作用。

Public Sub MoveFile() 

    Dim fso As Object 
    Dim sourceFile As String 
    Dim targetFile As String 

    ' You must add reference to "Microsoft Scripting Runtime" to your document 
    ' Tools > References... > scroll down the item. 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    sourceFile = "C:\Users\davidr\Desktop\foo.txt" 
    targetFile = "\\192.168.95.1:83\foo.txt" 

    ' Test if destination file already exists 
    If fso.FileExists(targetFile) Then 
     MsgBox ("This file exists!") 
     Exit Sub 
    End If 

    ' Move the file 
    fso.CopyFile sourceFile, targetFile 
    Set fso = Nothing 

End Sub