Uploading files with Windows.Networking.BackgroundTransfer

Uploading (or downloading) large files to your web server has always been a dangerous process. Too many users with large files can cause server overload, users trying to upload files beyond the accepted server size, giving reliable feedback to the user while uploading and handling file interruptions are common causes. So when I had to build uploading large files into a UWP app I really didn’t want to rely on the standard HTTP Post method. Thankfully there is an awesome API built in to the framework called Background Transfer.

Windows.Networking.BackgroundTransfer

Background Transfer has some really great features that tie in really well with the UWP framework, such as:

Background processing even if the app is suspended or terminated

Network sensitive by automatically suspending and then resuming when the network is available again

Data Sense and Battery Sense aware

Each transfer runs in a separate process

Progress information is easy to hook into

Full control over the process with pause, resume and cancel

Uploading files

Before we can write our UWP code we are going to need a server to upload the file to:

Create a new website project in Visual Studio: File->New Web Site…

Create an ASP.NET Empty Web Site

Create a new web form called Upload.aspx

Open Upload.aspx.vb and paste in the following code:

Partial Class Upload
Inherits System.Web.UI.Page
Private Sub Upload_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
'check if our header has the filename
If Request.Headers("Filename") IsNot Nothing Then
Dim fileName As String = Request.Headers("Filename")
'show the filename we are uploading
Response.Write(Convert.ToString("Filename is ") & fileName)
'set the save location
Dim saveLocation As String = Convert.ToString(Server.MapPath("UploadedData") + "\") & fileName
'read the file and save it
Using fs As New System.IO.FileStream(saveLocation, System.IO.FileMode.Create)
Request.InputStream.CopyTo(fs)
End Using
End If
Catch ex As Exception
Response.StatusCode = 500
Response.StatusDescription = ex.Message
Response.End()
End Try
End Sub
End Class

Create a new folder in the root of your web project called UploadedData. This where our uploaded files will be saved.

Run your website project and take note of the URL including the port number when it launches in your browser:

SUPER IMPORTANT
Don’t use the above code in a production environment. The server code will accept any file uploaded to it and this can pose a serious security risk. You should authenticate any uploads before they begin and also make sure you handle any uploaded files correctly such as filtering file types etc.

You will need to expand the above with your own functionality such as controlling which file the user can upload, checking the file size and processing the file on the server after its uploaded etc.

Handling the Background Task

If you look at line 55 in the above code you can see we are getting the status of the task by calling CurrentProgress.Status. You can handle the status here but remember that the UploadProgress sub is called on a thread outside of the UI so you’ll need to be careful what you do here.
You’ll also see on line 41 we are getting the response back from the server so you can check that too.

Checking for current Background Tasks

If the user navigates away from the app or even if the current frame and then comes back you are going to need to show them any background tasks that are currently still going.

You’ll need to check for any active transfers when your app or frame is navigated to for example. Call the CheckForActiveTransfers function which will in turn hook into any active tasks via the ResumeBackgroundTasks function: