Quantcast

System.Net.WebClient.UploadFileAsync Problem

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

System.Net.WebClient.UploadFileAsync Problem

subsembly
Hi all,

just when I thought all troubles with WebClient are gone, I encountered a new one. This time the compatibility problem is with the WebClient.UploadFileAsync() method. When using this method in order to upload a file, the other end receives a binary stream wrapped as follows (example):

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

[...actual file data...]

--------------8cea4d9249390f8--

However, in the real Windows .NET Framework the very same method transmits the net file data, without wrapping it like a form data post. Hence our old Windows file uploading code actually corrupts files when uploading from Mono for Android.

BTW: I also tried this same with MonoTouch, which behaves exactly like Mono for Android.

Best regards,

Andreas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

Jonathan Pryor-2
Please file a feature request at bugzilla.xamarin.com.

Thanks,
 - Jon

On Jan 19, 2012, at 10:25 AM, subsembly wrote:

> Hi all,
>
> just when I thought all troubles with WebClient are gone, I encountered a
> new one. This time the compatibility problem is with the
> WebClient.UploadFileAsync() method. When using this method in order to
> upload a file, the other end receives a binary stream wrapped as follows
> (example):
>
> --------------8cea4d9249390f8
> Content-Disposition: form-data; name="file"; filename="Android.sub"
> Content-Type: application/octet-stream
>
> [...actual file data...]
>
> --------------8cea4d9249390f8--
>
> However, in the real Windows .NET Framework the very same method transmits
> the net file data, without wrapping it like a form data post. Hence our old
> Windows file uploading code actually corrupts files when uploading from Mono
> for Android.
>
> BTW: I also tried this same with MonoTouch, which behaves exactly like Mono
> for Android.
>
> Best regards,
>
> Andreas
>
> --
> View this message in context: http://mono-for-android.1047100.n5.nabble.com/System-Net-WebClient-UploadFileAsync-Problem-tp5157961p5157961.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

subsembly
It's not a feature request. It's a SEVERE BUG in the Mono WebClient implementation!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

Gonzalo Paniagua Javier
On Thu, Jan 19, 2012 at 10:57 AM, subsembly <[hidden email]> wrote:
> It's not a feature request. It's a SEVERE BUG in the Mono WebClient
> implementation!

Ok. Since the world is going to end, I will take a look just to make
sure that it is just a common bug and can be fixed quickly ;-).

I will follow up in the bug report you filed
(http://bugzilla.xamarin.com/show_bug.cgi?id=2970)

Thanks.

-Gonzalo
_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

subsembly
Thank you very much.

Maybe I should explain my situation: I bought a MonoTouch + MonoDroid enterprise licence almost a year ago, when it was still with Novell. We already have two applications developed in C# for .NET 2.0 for Windows Desktop and also for the .NET Compact Framework for those good old no longer built Windows Mobile devices. One of the applications is about 250K lines of code of which we share about 150K lines between .NET for Windows, .NET Compact Framework, MonoTouch and Mono for Android. Right now my colleague who is responsible for iPhone development is making good progress using MonoTouch and the port is almost complete. Which is great. With Mono for Android things look not at all that good and we have often been at the verge of giving it up completely. Due to the many (now fixed) GC bugs it was almost impossible to use the partly ported Android app for more than a short while without crashing. Also the Debugger was unusably slow, if it worked at all. These problems were probably due to the large (?) scale of the project including background threads, Internet access and so on. I really hope that Mono for Android will soon come to the point of sophistication where MonoTouch already is and which enables us to actually create a releaseable product. Thank you for making this possible!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

Gonzalo Paniagua Javier
In reply to this post by Gonzalo Paniagua Javier
On Thu, Jan 19, 2012 at 11:10 AM, Gonzalo Paniagua Javier
<[hidden email]> wrote:
> On Thu, Jan 19, 2012 at 10:57 AM, subsembly <[hidden email]> wrote:
>> It's not a feature request. It's a SEVERE BUG in the Mono WebClient
>> implementation!
>
> Ok. Since the world is going to end, I will take a look just to make
> sure that it is just a common bug and can be fixed quickly ;-).

Turns out the world is not going to end today. 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 ("http://127.0.0.1:9000/"), "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:
 -----------------------8cea4b4aff65c6b
 Content-Disposition: form-data; name="file"; filename="uploadfile.cs"
 Content-Type: application/octet-stream

The same program run using Mono yields:
--------------8cea4b552ea3188
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 (...).


Cheers.

-Gonzalo
_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

Gonzalo Paniagua Javier
In reply to this post by subsembly
On Thu, Jan 19, 2012 at 11:29 AM, subsembly <[hidden email]> wrote:

> Thank you very much.
>
> Maybe I should explain my situation: I bought a MonoTouch + MonoDroid
> enterprise licence almost a year ago, when it was still with Novell. We
> already have two applications developed in C# for .NET 2.0 for Windows
> Desktop and also for the .NET Compact Framework for those good old no longer
> built Windows Mobile devices. One of the applications is about 250K lines of
> code of which we share about 150K lines between .NET for Windows, .NET
> Compact Framework, MonoTouch and Mono for Android. Right now my colleague
> who is responsible for iPhone development is making good progress using
> MonoTouch and the port is almost complete. Which is great. With Mono for
> Android things look not at all that good and we have often been at the verge
> of giving it up completely. Due to the many (now fixed) GC bugs it was
> almost impossible to use the partly ported Android app for more than a short
> while without crashing. Also the Debugger was unusably slow, if it worked at
> all. These problems were probably due to the large (?) scale of the project
> including background threads, Internet access and so on. I really hope that
> Mono for Android will soon come to the point of sophistication where
> MonoTouch already is and which enables us to actually create a releaseable
> product. Thank you for making this possible!

We at Xamarin are working to make the Mono for Android experience
better, and we value (and need!) your feedback.

Thank you!

-Gonzalo
_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

subsembly
In reply to this post by Gonzalo Paniagua Javier
Got me :-)

To my surprise your code does indeed add the MIME header stuff, even on the .NET Framework for Windows. So I went back to my real application code to check this out. I found the difference is that we are using "PUT" and not "POST" as the HTTP method.

If you change your line to

wc.UploadFileAsync (new Uri ("http://127.0.0.1:9000/"), "PUT", "uploadfile.cs", evt);

then .NET for Windows does no longer add the MIME headers. Whereas, Mono still does.

Cheers,

Andreas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: System.Net.WebClient.UploadFileAsync Problem

Gonzalo Paniagua Javier
On Thu, Jan 19, 2012 at 12:07 PM, subsembly <[hidden email]> wrote:
> Got me :-)

Hah. No, you got me!

>
> To my surprise your code does indeed add the MIME header stuff, even on the
> .NET Framework for Windows. So I went back to my real application code to
> check this out. I found the difference is that we are using "PUT" and not
> "POST" as the HTTP method.
>
> If you change your line to
>
> wc.UploadFileAsync (new Uri ("http://127.0.0.1:9000/"), "PUT",
> "uploadfile.cs", evt);
>
> then .NET for Windows does no longer add the MIME headers. Whereas, Mono
> still does.

I have just fixed the PUT problem in Mono master and updated the bug
report accordingly.

While the next release is being cooked, you now know that the
workaround is to use "POST" :-).

Thanks!

-Gonzalo
_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Loading...