将文件从 Flex 上传到 WCF REST Stream 问题(如何在 REST WS 中解码多部分表单发布) [英] Uploading file from Flex to WCF REST Stream issues (how to decode multipart form post in REST WS)

查看:19
本文介绍了将文件从 Flex 上传到 WCF REST Stream 问题(如何在 REST WS 中解码多部分表单发布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该系统是一个与 WCF REST Web 服务通信的 Flex 应用程序.我正在尝试将文件从 Flex 应用程序上传到服务器,但遇到了一些问题,希望这里有人能够提供帮助.我在 Flex 应用程序中使用 FileReference 来浏览和上传此处定义的文件:

The system is a Flex application communicating with a WCF REST web service. I am trying to upload a file from the Flex application to the server and am running into some issues I'm hoping someone here will be able to help out with. I'm using a FileReference in the Flex app to browse and upload the file as defined here:

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

然后我在 WCF REST Web 服务(使用 WCF 4 REST 服务的项目类型)中以流的形式接收文件(在调试器中显示为 System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream)

I am then receiving the file as a Stream (shows as System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream in the debugger) in the WCF REST web service (using project type of WCF 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 方法:How do我在 C# 中将流保存到文件中?

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

文件保存没有任何问题.我的问题是该文件包含的信息比我想要的多.以下是保存文件的内容(其中源文件仅包含这是文件的内容"):

The file saves without any issues. The issue I have is that the file contains more information than i would like. Here are the contents of the saved file (where the source file only contained "THIS IS THE CONTENT OF THE FILE"):

------------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--

内容与 Adob​​e 文档中描述的完全相同:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html

The contents look exactly like they are described in the Adobe documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html

C# 中是否有任何工具可以从 Stream 中获取文件内容?

Are there any facilities in C# to get the file contents from the Stream?

编辑(3/24 8:15 pm):Flex 应用程序发送的是多部分表单 POST.如何解码由 Stream 参数表示的多部分正文数据并剥离多部分正文的片段?

EDIT (3/24 8:15 pm): What the Flex app is sending is a Multipart form POST. How can I decode the multipart body data as represented by the Stream parameter and strip out the pieces of the multipart body?

编辑(3/25 上午 10 点):更多相关的 Stack Overflow 帖子:
WCF 服务接受后编码的 multipart/form-data
将 multipart/form-data 发布到WCF REST 服务:动作变化

EDIT (3/25 10 am): some more Stack Overflow posts that are related:
WCF service to accept a post encoded multipart/form-data
POSTing multipart/form-data to a WCF REST service: the action changes

EDIT(3/25 10:45 am):找到一个运行良好的多部分解析器:
http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

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

提前致谢.

推荐答案

感谢 Anthony 在 http://antscode.blogspot.com/ 适用于效果很好的多部分解析器(用于图像、txt 文件等).

Thanks to Anthony over at http://antscode.blogspot.com/ for the multipart parser that works great (for images, txt files, etc).

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

这篇关于将文件从 Flex 上传到 WCF REST Stream 问题(如何在 REST WS 中解码多部分表单发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆