Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

InvalidOperationException: Response Content-Length mismatch when streaming a bitmap #2070

Closed
pathcore-tasos opened this issue Sep 20, 2017 · 5 comments

Comments

@pathcore-tasos
Copy link

Hi there,

If one tries to return an image in a FileStreamResult, the application will throw an exception along the lines of:
InvalidOperationException: Response Content-Length mismatch: too few bytes written (0 of 1234567)

My code was as follows:

private static IActionResult ImageResult(Bitmap image, ImageFormat format, string mimeType)
{
    var stream = new MemoryStream();
    image.Save(stream, format);
    return new FileStreamResult(stream, mimeType);
}

However, if I changed it to the following, the image is returned without complaint:

private static IActionResult ImageResult(Bitmap image, ImageFormat format, string mimeType)
{
    var stream = new MemoryStream();
    image.Save(stream, format);
    return new FileContentResult(stream.ToArray(), mimeType);
}

It appears that doing the reading manually fixes the issue.

Thanks!

@Tratcher
Copy link
Member

Did you forget to Seek the stream back to the beginning?

@pathcore-tasos
Copy link
Author

Yep that would do it!

Sorry to take up your time!

@HWouters
Copy link

HWouters commented Oct 3, 2017

I get the same exception one out of two requests. It only happens when I deploy my application to Azure app service. Unfortunately, I can't reproduce it locally.

public class HomeController : Controller
{
    public IActionResult Spa()
    {
        return File("~/index.html", "text/html");
    }
}

@Tratcher
Copy link
Member

Tratcher commented Oct 3, 2017

@HWouters that looks like a different issue. See aspnet/Mvc#6875

@HWouters
Copy link

HWouters commented Oct 4, 2017

@Tratcher thank you for pointing me to the right issue. The workaround solves my problem until the patch is released.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants