Loading a file from Flex to WCF REST Stream problems (how to decode multi-page recording in REST WS)

The system is a Flex application that interacts with the WCF REST web service. I am trying to upload a file from a Flex application to a server, and I am encountering some problems, I hope someone here can help. I am using FileReference in a Flex application to view and download a file as defined here:

http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/

Then I get the file as Stream (displayed as System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream in the debugger) in the WCF REST web service (using the WCF 4 REST service project type)

[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); } } 

Note. CopyStream method used in this post: How to save a stream to a file in C #?

The file is saved without any problems. The problem is that the file contains more information than we would like. Here is the contents of the saved file (where the source file contains only "THIS FILE CONTENT"):

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

The content looks as described in the Adobe documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html

Is there any means in C # to get the contents of a file from Stream?

EDIT (3/24 8:15 pm) : what the Flex application sends is a multi-page POST form. How can I decode multiple body data represented by the Stream parameter and cut out pieces of the multi-body?

EDIT (3/25 10:00) : A few more stack overflow messages that are related:
WCF service to receive encrypted messages / form data POSTing multipart / form-data to WCF REST service: action changes

EDIT (3/25 10:45 AM) : Found a multi-parser parser that works very well:
http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

Thanks in advance.

+4
c # flex rest stream wcf
source share
3 answers

Thanks Anthony on http://antscode.blogspot.com/ for a multi-frame parser that works great (for images, txt files, etc.).

http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

+5
source share

I open the C # Http open parser here .

This is slightly more flexible than the one mentioned above, which applies to CodePlex, since you can use it for both Multipart and non-Multipart form-data , and also provides other form parameters formatted in the Dictionary object.

This can be used as follows:

non-frequent

 public void Login(Stream stream) { string username = null; string password = null; HttpContentParser parser = new HttpContentParser(data); if (parser.Success) { username = HttpUtility.UrlDecode(parser.Parameters["username"]); password = HttpUtility.UrlDecode(parser.Parameters["password"]); } } 

of many

 public void Upload(Stream stream) { HttpMultipartParser parser = new HttpMultipartParser(data, "image"); if (parser.Success) { string user = HttpUtility.UrlDecode(parser.Parameters["user"]); string title = HttpUtility.UrlDecode(parser.Parameters["title"]); // Save the file somewhere File.WriteAllBytes(FILE_PATH + title + FILE_EXT, parser.FileContents); } } 
+6
source share

I had some problems with the parser, which are based on parsing strings, especially with large files. I found that it would run out of memory and would not process binary data.

To deal with these problems, I opened my own attempt for the C # multipart / form-data parser here

See my answer here for more details.

+1
source share

All Articles