Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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
Posted
v2

1 solution

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

Try using

C#
Server.MapPath(imageUrl);

or
HttpContext.Current.Server.MapPath(imageUrl)

Happy Coding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900