c# - 将文件从 Flex 上传到 WCF REST Stream 问题(如何解码 REST WS 中的多部分表单发布)

标签 c# wcf apache-flex rest stream

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

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)

[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 I save a stream to a file in C#?

文件保存没有任何问题。我遇到的问题是该文件包含的信息比我想要的要多。以下是已保存文件的内容(源文件仅包含“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

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

编辑(3 月 24 日晚上 8:15):Flex 应用程序发送的是一个多部分表单 POST。如何解码由 Stream 参数表示的多部分正文数据并去除多部分正文的片段?

编辑(3 月 25 日上午 10 点):更多相关的 Stack Overflow 帖子:
WCF service to accept a post encoded multipart/form-data
POSTing multipart/form-data to a WCF REST service: the action changes

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

提前致谢。

最佳答案

我开源了一个 C# Http 表单解析器 here .

这比 CodePlex 上提到的另一个稍微灵活一些,因为您可以将它用于多部分和非多部分 form-data,并且它还为您提供其他格式化的表单参数在 Dictionary 对象中。

这可以按如下方式使用:

非多部分

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"]);
    }
}

多部分

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

关于c# - 将文件从 Flex 上传到 WCF REST Stream 问题(如何解码 REST WS 中的多部分表单发布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5423031/

相关文章:

c# - 无法在 SQL c# 中更新表

asp.net - "ServiceHost only supports class service types"

apache-flex - 禁用按钮栏中的单个按钮

apache-flex - Flex 和亚马逊支付

c# - 为什么我的字符串格式化程序在此多重绑定(bind)中被忽略?

c# - 如何在两个位置之间连续移动 GameObject?

c# - WithRequired() 没有定义扩展方法

c# - rpc\\literal SOAP 不支持 namespace 。包装器元素必须是不合格的

.net - WCF netTCPBinding - 传输加密是否足够?

apache-flex - Flex 中的基本名称?