2016-05-09 24 views
0

私のvb.netウィンドウフォームアプリケーションでは、ユーザーが選択したドキュメントをサーバーにアップロードする機能を追加する必要があります。サービスを使用してサーバーにファイルをアップロードします。アプリケーションを実行すると、選択したファイルがサーバーにアップロードされず、リモートサーバーに接続できないというメッセージが表示されます。私はリモートサーバーにファイルをアップロードするためにWebサービスを使用することについてたくさん検索しましたが、何も私のために働いていませんでした。私は窓からサーバーにファイルをアップロードすることができますどのようにアプリケーションが..any助けが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 

答えて

関連する問題