问题描述
我正在尝试使用OpenRasta发布一些文件.我已经达到调用处理程序的程度,但是从外观上看,实体中的流是空的.这是我的处理程序:
I'm trying to post some files using OpenRasta. I've gotten as far as getting my handler called, but by all appearances the stream in the entity is empty. Here's my handler:
public OperationResult Post( IEnumerable<IMultipartHttpEntity> entities)
{
var foo = entities.ToList();
foreach (var entity in foo)
{
if (entity.Stream != null && entity.ContentType != null)
{
var memoryStream = new MemoryStream();
entity.Stream.CopyTo(memoryStream);
}
}
return new OperationResult.Created();
}
每次循环memoryStream的长度为0.我在做什么错了?
Each time through the loop memoryStream has a length of 0. What am I doing wrong?
推荐答案
没有什么比在StackOverflow上发布使答案立即显而易见的了.显然,您只有一个实体的枚举才能获取流.我在上面添加了"foo"变量以使调试更加容易,但是这导致流式传输失败.当我将流存储到数据库时,在写入之前,我也未能将memoryStream重置为开始.解决了这两个问题后,文件就可以正确上传了.
Nothing like posting on StackOverflow to make the answer immediately obvious. Apparently you only get one enumeration of the entities in order to grab the stream. I had added the "foo" variable above to make debugging easier, but it was causing the streaming to fail. As I stored the stream to the database, I had also failed to reset memoryStream to the beginning before writing it. Fixing these two issues got the file to upload correctly.
这篇关于使用OpenRasta和IMultipartHttpEntity上传多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!