簡體   English   中英

將文件從Flex上傳到WCF REST Stream問題(如何在REST WS中解碼多部分表單帖子)

[英]Uploading file from Flex to WCF REST Stream issues (how to decode multipart form post in REST WS)

該系統是與WCF REST Web服務通信的Flex應用程序。 我正在嘗試將文件從Flex應用程序上傳到服務器並遇到一些問題,我希望有人可以幫忙。 我在Flex應用程序中使用FileReference來瀏覽和上傳此處定義的文件:

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

然后,我在WCF REST Web服務中使用流(在調試器中顯示為System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream)接收文件(使用WCF 4 REST服務的項目類型)

[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方法: 如何將流保存到C#中的文件?

該文件保存沒有任何問題。 我遇到的問題是該文件包含的信息比我想要的多。 以下是保存文件的內容(源文件僅包含“這是文件的內容”):

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

內容與Adobe文檔中描述的內容完全相同: http//help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html

C#中是否有任何工具可以從Stream中獲取文件內容?

編輯(3/24 8:15 pm) :Flex應用程序發送的是一個Multipart表單POST。 如何解碼由Stream參數表示的多部分正文數據並刪除多部分正文的各個部分?

編輯(3/25上午10點) :一些相關的Stack Overflow帖子:
WCF服務接受post編碼的multipart / form-data
將multipart / form-data發布到WCF REST服務:操作更改

編輯(3/25上午10:45) :找到一個非常好的多部分解析器:
http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

提前致謝。

在這里開源了一個C#Http表單解析器。

這比在CodePlex上提到的另一個稍微靈活一些,因為您可以將它用於Multipart和非Multipart 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);
    }
}

感謝Anthony在http://antscode.blogspot.com/上為多部分解析器工作得很好(對於圖像,txt文件等)。

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

我有一些基於字符串解析的解析器問題,特別是對於大文件我發現它會耗盡內存而無法解析二進制數據。

為了應對這些問題,我已經開源了在C#中的multipart / form-data的解析器我自己嘗試在這里

見我的答案在這里了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM