Click here to Skip to main content
15,434,822 members
Articles / Desktop Programming / Windows Forms
Posted 14 May 2009


30 bookmarked

How to Upload a File to a WebDAV Server in VB.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
14 May 2009CPOL2 min read
This article demonstrates how to upload a file to a (HTTPS) WebDAV server in VB.NET.

Image 1


My company recently moved our FTP site to a secure WebDAV server (Web-based Distributed Authoring and Versioning). I was tasked with developing some automated download software, which lead to uploading as well. I thought there would be plenty of examples on the internet on how to do this, but was surprised to realize the difficulty I was having finding enough information. Because it took me a while to piece it together, I thought I would share my solution with others who may have similar needs.

Although there may be different ways for accessing and downloading information from a WebDAV server, I chose to use HTTPS, and have built this demo around that.

To learn how to download a file from a WebDAV server, please see my article How to Download a File from a WebDAV server in VB.NET.


If you are familiar with HttpWebRequest and HttpWebResponse, then you should feel right at home. The only difference between a regular server request and a WebDAV server request is that the "Translate: f" header must be added, along with setting SendChunks = True and AllowWriteStreamBuffering = True.

If you're new to uploading files to a WebDAV server, then just follow along. I've included a demo that you can download and walk through to see how it works. The demo was created with VS 2008, Visual Basic, and .NET Framework 2.0.

Using the code

The first thing we need to do is get the path of the file, and its length.

Dim fileToUpload As String = "c:\temp\transfer"
Dim fileLength As Long = My.Computer.FileSystem.GetFileInfo(fileToUpload).Length

Next, we need to get the URL and the port, and combine them if a port was provided.

Dim url As String = ""
Dim port As String = "443"

'If the port was provided, then insert it into the url.
If port <> "" Then

     'Insert the port into the Url
     Dim u As New Uri(url)

     'Get the host (example: "")
     Dim host As String = u.Host

     'Replace the host with the host:port
     url = url.Replace(host, host & ":" & port)

End If

Add the name of the file we are uploading to the end of the URL. This creates a "target" file name.

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

Create a request for the WebDAV server for the file we want to upload.

Dim userName As String = "UserName"
Dim password As String = "Password"

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

'Set the User Name and Password
request.Credentials = New NetworkCredential(userName, password)

'Let the server know we want to "put" a file on it
request.Method = WebRequestMethods.Http.Put

'Set the length of the content (file) we are sending
request.ContentLength = fileLength

'*** This is required for our WebDav server ***
request.SendChunked = True
request.Headers.Add("Translate: f")
request.AllowWriteStreamBuffering = True

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

After the server has given us a stream, we can begin to write to it.

Note: The data is not actually being sent to the server here, it is written to a stream in memory. The data is actually sent below when the response is requested from the server.

'Open the file so we can read the data from it
Dim fs As New IO.FileStream(fileToUpload, IO.FileMode.Open, _

'Create the buffer for storing the bytes read from the file
Dim byteTransferRate As Integer = 1024
Dim bytes(byteTransferRate - 1) As Byte
Dim bytesRead As Integer = 0
Dim totalBytesRead As Long = 0

'Read from the file and write it to the server's stream.
    '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)

    End If

Loop While bytesRead > 0

'Close the server stream
s = Nothing

'Close the file
fs = Nothing

Now, although we have finished writing the file to the stream, the file has not been uploaded yet. If we exit here without continuing, the file would not be uploaded.

The last step we have to perform is sending the data to the server.

  • When we request a response from the server, we are actually sending the data to the server, and receiving the server's response in return.
  • If we do not perform this step, the file would not be uploaded.
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Finally, after we have uploaded the file, perform a little validation just to make sure everything worked as expected.

'Get the StatusCode from the server's Response
Dim code As HttpStatusCode = response.StatusCode

'Close the response
response = Nothing

'Validate the uploaded file.
' Check the totalBytesRead and the fileLength: Both must be an exact match.
' Check the StatusCode from the server and make sure the file was "Created"
' Note: There are many different possible status codes. You can choose
' which ones you want to test for by looking at the "HttpStatusCode" enumerator.
If totalBytesRead = fileLength AndAlso _
    code = HttpStatusCode.Created Then

    MessageBox.Show("The file has uploaded successfully!", "Upload Complete", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)


    MessageBox.Show("The file did not upload successfully.", _
        "Upload Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning)

End If


I hope this demo helps you out in your endeavors!

Remember to see my other article: How to download a File from a WebDAV Server in VB.NET.


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

Written By
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

QuestionThanks a ton Pin
Member 36698899-Sep-18 1:25
MemberMember 36698899-Sep-18 1:25 
SuggestionLarger Files Pin
Member 1029413831-Jul-14 7:25
MemberMember 1029413831-Jul-14 7:25 
GeneralWorks like a charm Pin
D. de Kerf2-Apr-13 21:31
MemberD. de Kerf2-Apr-13 21:31 
QuestionWebDAV Pin
KevinBrady5711-Oct-12 6:15
MemberKevinBrady5711-Oct-12 6:15 
AnswerRe: WebDAV Pin
CS Rocks11-Oct-12 6:48
MemberCS Rocks11-Oct-12 6:48 
GeneralRe: WebDAV Pin
Titto44145-Jun-19 20:26
MemberTitto44145-Jun-19 20:26 
GeneralMy vote of 5 Pin
Omar Khaled Mekkawy28-Sep-12 0:53
MemberOmar Khaled Mekkawy28-Sep-12 0:53 
GeneralMy vote of 5 Pin
Daniel Leykauf28-Jul-12 3:40
MemberDaniel Leykauf28-Jul-12 3:40 
QuestionOverwrite option Pin
Gian Andrea21-Feb-12 23:00
MemberGian Andrea21-Feb-12 23:00 
GeneralOutOfMemory exception Pin
vit vit22-Feb-11 3:31
Membervit vit22-Feb-11 3:31 
GeneralGreat article VBRocks, but one question... Pin
jportelas23-Dec-10 1:26
Memberjportelas23-Dec-10 1:26 
GeneralError 501 Not Implenented Errors Pin
Pokey8417-Oct-10 9:01
MemberPokey8417-Oct-10 9:01 
QuestionHow can I use a progressbar with it? Pin
rezanew26-Jun-10 8:13
Memberrezanew26-Jun-10 8:13 
GeneralExcellent example Pin
Goran Bacvarovski19-Feb-10 8:28
MemberGoran Bacvarovski19-Feb-10 8:28 
GeneralI get response 200 OK. Pin
mrdance18-Jan-10 11:12
Membermrdance18-Jan-10 11:12 
GeneralRe: I get response 200 OK. Pin
Goran Bacvarovski19-Feb-10 8:22
MemberGoran Bacvarovski19-Feb-10 8:22 
GeneralRe: I get response 200 OK. Pin
bidalah29-Oct-16 4:57
Memberbidalah29-Oct-16 4:57 
GeneralC# Pin
himanshubpatel29-Oct-09 5:24
Memberhimanshubpatel29-Oct-09 5:24 
GeneralUpload File to a WebServer via https Pin
noobdev14-Jul-09 23:45
Membernoobdev14-Jul-09 23:45 
AnswerRe: Upload File to a WebServer via https Pin
mmjd2-Aug-09 21:03
Membermmjd2-Aug-09 21:03 
GeneralRe: Upload File to a WebServer via https Pin
Goran Bacvarovski19-Feb-10 8:13
MemberGoran Bacvarovski19-Feb-10 8:13 
QuestionError 409 Pin
Ase229-May-09 0:10
MemberAse229-May-09 0:10 
AnswerRe: Error 409 Pin
Goran Bacvarovski19-Feb-10 8:27
MemberGoran Bacvarovski19-Feb-10 8:27 
Generalthanks Pin
Jeff Circeo16-May-09 3:23
MemberJeff Circeo16-May-09 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.