如何通过http将文件从虚拟目录上传到服务器? [英] How to Upload files from virtual directory to server through http?

查看:74
本文介绍了如何通过http将文件从虚拟目录上传到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



如果你解决了我的疑问,你会非常感激..



i正在开发窗口应用程序,通过http..now将文件从本地磁盘上传到特定服务器。我已经将文件上传到虚拟目录的位置。如何将其发送到服务器?





任何人都可以通过网络服务了解这个过程吗?



请参考我的代码



Dear friends,

You will be very thankful if you solve my doubt..

i am developing window application to upload files from local disk to particcular server through http..now i have upload the file in the location of virtual directory.how to send it in server?


can anybody have idea on webservices to achieve this process?

please refer my code

Public Sub UploadFile()

    Dim fileToUpload As String = Me.txtFileToUpload.Text.Trim()
    Dim fileLength As Long = My.Computer.FileSystem.GetFileInfo(fileToUpload).Length
    Dim url As String = Me.txtUrl.Text.Trim()
    Dim port As String = Me.txtPort.Text.Trim()

    If port <> "" Then
        Dim u As New Uri(url)
        Dim host As String = u.Host
        url = url.Replace(host, host & ":" & port)

    End If

    url = url.TrimEnd("/"c) & "/" & IO.Path.GetFileName(fileToUpload)

    Dim request As HttpWebRequest = _
        DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)

    request.Method = WebRequestMethods.Http.Put
    request.ContentLength = fileLength
    request.SendChunked = True
    request.Headers.Add("Translate: f")
    request.AllowWriteStreamBuffering = True


    Dim s As IO.Stream = Nothing

    Try
        'Send the request to the server, and get the
        '  server's (file) Stream in return.
        s = request.GetRequestStream()

    Catch ex As Exception

        MessageBox.Show(ex.StackTrace, "An error occurred while attempting to connect to the remote server.  " & _
                            "The following error occurred while attempting to connect:" & vbCrLf & _
                            vbCrLf & ex.Message)

    End Try
    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
    Dim totalBytesRead As Long = 0
    pb.Minimum = 0
    pb.Maximum = CInt(fileLength \ byteTransferRate)
    pb.Value = 0
    Do
        'Read from the file
        bytesRead = fs.Read(bytes, 0, bytes.Length)

        If bytesRead > 0 Then

            totalBytesRead += bytesRead


            'Write to stream
            s.Write(bytes, 0, bytesRead)

            'Update progress
            pb.Value = CInt(totalBytesRead \ byteTransferRate)

            If pb.Value Mod 500 = 0 Then _
                Application.DoEvents()

        End If

    Loop While bytesRead > 0

    'Close the server stream
    s.Close()
    s.Dispose()
    s = Nothing

    'Close the file
    fs.Close()
    fs.Dispose()
    fs = Nothing

    Dim response As HttpWebResponse = Nothing

    response = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim code As HttpStatusCode = response.StatusCode
    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

推荐答案

您需要将目录的虚拟路径映射到服务器的实际路径。



尝试使用



You need to map the virtual path of the directory to the actual path of the server.

Try using

Server.MapPath(imageUrl);






or

HttpContext.Current.Server.MapPath(imageUrl)



快乐编码


Happy Coding


这篇关于如何通过http将文件从虚拟目录上传到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆