Bug 2970 - System.Net.WebClient.UploadFileAsync is broken
Summary: System.Net.WebClient.UploadFileAsync is broken
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.0
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-01-19 11:06 UTC by info
Modified: 2012-01-19 12:25 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description info 2012-01-19 11:06:16 UTC
The Mono implementation of the WebClient.UploadFileAsync() method falsely wraps the uploaded data with a form data encoding header and trailer. For example:

Content-Disposition: form-data; name="file"; filename="Android.sub"
Content-Type: application/octet-stream

[...actual file data...]


Instead it must send the raw file data without any header or trailer, just like the real Windows .NET Framework 2.0 implementation does. By adding the header and trailer existing Windows file uploading code actually corrupts files when uploading from Mono for Android.

BTW: I also tried this same with MonoTouch, which makes exactly the same mistake like Mono for Android.
Comment 1 Gonzalo Paniagua Javier 2012-01-19 11:38:42 UTC
This is not a bug.

The following program:

using System;
using System.Net;
using System.Threading;

class Test {
	static void Main ()
		ManualResetEvent evt = new ManualResetEvent (false);
		WebClient wc = new WebClient ();
		wc.UploadFileCompleted +=  new UploadFileCompletedEventHandler (CB);
		wc.UploadFileAsync (new Uri (""), "POST", "uploadfile.cs", evt);
		evt.WaitOne ();

	static void CB (object sender, UploadFileCompletedEventArgs e)
		if (!e.Cancelled && e.Error == null) {
			Console.WriteLine ("File uploaded");
		} else {
			Console.WriteLine ("Something went wrong");
			Console.WriteLine (e.Error);
		((ManualResetEvent) e.UserState).Set ();


when run under MS.NET has this at the beginning of the body:
 Content-Disposition: form-data; name="file"; filename="uploadfile.cs"
 Content-Type: application/octet-stream

The same program run using Mono yields:
Content-Disposition: form-data; name="file"; filename="uploadfile.cs"
Content-Type: application/octet-stream

Perhaps what you want is more like:
   file.ReadAllBytes ("yourfile");
   wc.UploadData (...).
Comment 2 Gonzalo Paniagua Javier 2012-01-19 12:25:57 UTC
Turns out that using "PUT" instead of "POST" sends the file as-is without headers.

The fix is in master/c621b27 and will soon be backported to mono-2-10, where Mono for Android and MonoTouch will pick it up.