Uploading multiple files with C# 14

Have you ever been in a situation where you needed to upload multiple files to a remote host and pass additional parameters in the request? Unfortunately there’s nothing in the BCL that allows us to achieve this out of the box.

We have the UploadFile method but it is restricted to a single file and doesn’t allow us to pass any additional parameters. So let’s go ahead and write such method. The important part is that this method must comply with RFC 1867 so that the remote web server can successfully parse the information.

First we define a model representing a single file to be uploaded:

And here’s a sample UploadFiles method implementation:

And here’s a sample usage:

In this example we are uploading 3 values and 3 files to the remote host.

Next time I will show how to improve this code by adding an asynchronous version using the TPL library in .NET 4.0.

Leave a comment

Your email address will not be published. Required fields are marked *

14 thoughts on “Uploading multiple files with C#

  • Nghiem Hoang

    I followed your source code but it throws exception “The remote server returned an error: (405) Method Not Allowed.”
    Do I have to configure anything else on the IIS server? I’m using IIS 7 and windows 7.
    I create a folder as your example “http://localhost:8080/Upload” and map to “C:\inetpub\site1.com\Upload”

  • Colin Howe

    I have spent the last two days at work trying to get a concise and relevant explantion of this simple process, can i say thankyou. You have no idea, brilliant, simple works liek a charm

  • Cphilo

    I just try your code to upload image data to server like this:
    var files = new[]
    new UploadFile {
    Name = “screenshot”,
    Filename = “screenshot.jpg”,
    ContentType = “image/jpeg”,
    Stream = ms
    var parameters = new NameValueCollection
    { “username”, uname },
    { “password”, pwd },
    { “type”, “screen” },
    { “id”, screen_id },
    byte[] bytes = UploadFiles(uploadUrl, files, parameters);

    The result is that the parameters can be detected by the server, but the files data cannot be detected and return 500 error. I finally cannot figure out just why this cannot work. Do you have some ideas about this?

  • Emad

    please sample php code for get file in your code.
    send file with C# to PHP and save in webserver Directory.

    send for example:
    user (Get with $_POST in php for echo user)
    file.avi (Get with $_FILES in php for save file)

    and Show log in C#:
    return to C# for show Result (File sented or user is Wrong or file send error Retry again(button for retry))

  • Luis Saenz

    Dear to ask us if you have an example or can guide me to upload files and data to see from a desktop application with a Apacha c # and php servidro
    agradecere your support thanks

  • c#noob

    Thank you so much for this example code.I’m new to c# and tried to write an upload with the httpclient implementation and failed so often. 🙂 Wonderful !!!

  • Dee_Knox

    Very nice. How will the uri on the receiving end handle the content though? That is separating the bytes into the 3 files and 3 additional parameters. I really need help on that

  • Peter

    Thanks man,

    You saved my week! I was struggling with a post and never noticed the extra 2 dashes (–) as an escape sequence before the actual boundary!
    You are a hero!