Introduction
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.
Background
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 me.zip"
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 = "https://someSecureTransferSite.com/directory"
Dim port As String = "443"
If port <> "" Then
Dim u As New Uri(url)
Dim host As String = u.Host
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"
Dim request As HttpWebRequest = _
DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
request.Credentials = New NetworkCredential(userName, password)
request.Method = WebRequestMethods.Http.Put
request.ContentLength = fileLength
request.SendChunked = True
request.Headers.Add("Translate: f")
request.AllowWriteStreamBuffering = True
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.
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
Do
bytesRead = fs.Read(bytes, 0, bytes.Length)
If bytesRead > 0 Then
totalBytesRead += bytesRead
s.Write(bytes, 0, bytesRead)
End If
Loop While bytesRead > 0
s.Close()
s.Dispose()
s = Nothing
fs.Close()
fs.Dispose()
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.
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
Conclusion
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.