如何使用Web服务从Windows窗体应用程序上传文件到服务器

时间:2016-05-09 09:56:47

标签: vb.net winforms web-services file-upload

在我的vb.net windows窗体应用程序中,我需要添加用户将所选文档上传到服务器的功能,我已经使用Web服务将文件上传到服务器。但是当我运行应用程序时,所选文件未上传到服务器,无法连接到远程服务器消息。我已经搜索了很多关于使用Web服务将文件上传到远程服务器的信息,但对我来说没什么用。请问如何从Windows窗体应用程序上传文件到服务器请帮助我..非常感谢

我的代码

网络服务

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.IO
Imports System.Data
Imports System.Web
Imports System.Collections

 <System.Web.Services.WebService(Namespace:="Uploader")> _
     <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>     _
<ToolboxItem(False)> _
 Public Class FileUploader
 Inherits System.Web.Services.WebService

<WebMethod()> _
 Public Function UploadFile(ByVal f As Byte(), ByVal fileName As String) As String

'the byte array argument contains the content of the file the string argument contains the name and extension of the file passed in the byte array
Try

    ' instance a memory stream and pass the byte array to its constructor
    Dim ms As MemoryStream = New MemoryStream(f)
    'instance a filestream pointing to the storage folder, use the original file name to name the resulting file

    Dim serverIp as string = "10.  .  . \E:\UploadedFiles\"

    Dim fs As FileStream = New FileStream(System.Web.Hosting.HostingEnvironment.MapPath(serverIp) + fileName, FileMode.Create)

    'write the memory stream containing the original file as a byte array to the filestream
    ms.WriteTo(fs)


    'clean up
    ms.Close()
    fs.Close()
    fs.Dispose()

    'return OK if we made it this far
    Return "OK"

Catch ex As Exception
    ' return the error message if the operation fails
    Return ex.Message.ToString()

End Try


End Function

End Class

上传File.vb

Private Sub UploadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadFile.Click


'Show  FileDialog to select a file by the user 
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String = ""

fileDialog.Title = "Open File Dialog"
fileDialog.InitialDirectory = "C:\"
fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fileDialog.FilterIndex = 2
fileDialog.RestoreDirectory = True

If fileDialog.ShowDialog() = DialogResult.OK Then
    strFileName = fileDialog.FileName
End If

MsgBox(strFileName, MsgBoxStyle.Information, "strFileName")

If (strFileName <> String.Empty) Then
    UploadFile(strFileName)
Else
    MessageBox.Show("You must select a file first.", "No File Selected")

End If
End Sub


 Private Sub UploadFile(ByVal filename As String)

Try

    'get the exact file name from the path
    Dim strFile As String = System.IO.Path.GetFileName(filename)
    ' create an instance fo the web service
    Dim srv As Uploader.FileUploaderService = New Uploader.FileUploaderService()
    'get the file information form the selected file
    Dim fInfo As FileInfo = New FileInfo(filename)
    'get the length of the file to see if it is possible to upload it (with the standard 4 MB limit)
    Dim numBytes As Long = fInfo.Length
    Dim dLen As Double = Convert.ToDouble(fInfo.Length / 1000000)

    'Default limit of 4 MB on web server have to change the web.config to if you want to allow larger uploads
    If (dLen < 4) Then
        'set up a file stream and binary reader for the  selected file
        Dim fStream As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read)
        Dim br As BinaryReader = New BinaryReader(fStream)

        'convert the file to a byte array
        Dim data As Byte() = br.ReadBytes(numBytes)
        br.Close()

        ' pass the byte array (file) and file name to the web service()
        Dim sTmp As String = srv.UploadFile(data, strFile)
        fStream.Close()
        fStream.Dispose()

        ' this will always say OK unless an error occurs, if an error occurs, the service returns the error Message()
        MessageBox.Show("File Upload Status: " + sTmp, "FileUpload")


    Else
        'Display message if the file was too large to upload
        MessageBox.Show("The file selected exceeds the size limitfor uploads.", "File Size")

    End If

Catch ex As Exception

    MessageBox.Show(ex.Message.ToString(), "Upload Error")
End Try




End Sub

0 个答案:

没有答案