Ed *_*nek 4 c# apache-flex rest wcf stream
该系统是与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);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:此帖子中使用的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--
Run Code Online (Sandbox Code Playgroud)
内容与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
POSTing 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和Non-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"]);
}
}
Run Code Online (Sandbox Code Playgroud)
多
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);
}
}
Run Code Online (Sandbox Code Playgroud)
感谢Anthony在http://antscode.blogspot.com/上为多部分解析器工作得很好(对于图像,txt文件等).
http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html
| 归档时间: |
|
| 查看次数: |
9015 次 |
| 最近记录: |