2013-11-15 47 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