Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3701

How do I decode POST multipart/form-data?

$
0
0

In a Flex application communicating with a Windows Communication Foundation REST web service I am trying to upload a file from the Flex application to the server. I'm using FileReference to browse and upload the file.

I am then receiving the file as a stream (shows as System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream in the debugger) using project type of Windows Communication Foundation 4 REST service.

[WebInvoke(Method = "POST", UriTemplate = "_test/upload")]public void UploadImage(Stream data){    // TODO: just hardcode filename for now    var filepath = HttpContext.Current.Server.MapPath(@"~\_test\testfile.txt");    using (Stream file = File.OpenWrite(filepath))    {        CopyStream(data, file);    }}private static void CopyStream(Stream input, Stream output){    var buffer = new byte[8 * 1024];    int len;    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)    {        output.Write(buffer, 0, len);    }}

CopyStream method used from this post. The file contains more information than I would like (the source file only contained "THIS IS THE CONTENT OF THE FILE"):

------------ae0ae0Ef1ae0Ef1ae0gL6gL6Ij5cH2Content-Disposition: form-data; name="Filename"testfile.txt------------ae0ae0Ef1ae0Ef1ae0gL6gL6Ij5cH2Content-Disposition: form-data; name="Filedata"; filename="testfile.txt"Content-Type: application/octet-streamTHIS IS THE CONTENT OF THE FILE------------ae0ae0Ef1ae0Ef1ae0gL6gL6Ij5cH2Content-Disposition: form-data; name="Upload"Submit Query------------ae0ae0Ef1ae0Ef1ae0gL6gL6Ij5cH2--

The contents look exactly like described in the Adobe documentation. Are there any facilities in C# to get the file contents from the stream? The Flex app is sending a multipart/form-data POST. How can I decode the multipart body data as represented by the Stream parameter and strip the pieces of the multipart body?


Viewing all articles
Browse latest Browse all 3701

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>