Uploading with multipart/form-data using OpenRasta and IMultipartHttpEntity

眉间皱痕 提交于 2019-12-13 01:59:16

问题


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

Each time through the loop memoryStream has a length of 0. What am I doing wrong?


回答1:


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.



来源:https://stackoverflow.com/questions/4190537/uploading-with-multipart-form-data-using-openrasta-and-imultiparthttpentity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!