-->

Uploading with multipart/form-data using OpenRasta

2019-08-06 13:04发布

问题:

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.