Image URL.Action OutputStream is not available when a custom TextWriter is used

Topics: MVC, Troubleshooting
Aug 2, 2011 at 5:30 PM

I am trying to implement image tag in a mvc view

<img src="@Url.Action("AvatarImage", "Profile",1)" alt="avatar" id="imgCropImage" /> 

In my controller i am create a separate class as suggested

http://stackoverflow.com/questions/2261198/binarywrite-exception-outputstream-is-not-available-when-a-custom-textwriter-is

I am however still getting error "OutputStream is not available when a custom TextWriter is used" when trying to call response.BinaryWrite(image);.

Is there a work around i should be using?

As always much appreciate the help.

Minh

 

Aug 3, 2011 at 6:27 PM

It looks like in the MVC "Player.cs" creates a string writter around line 45 that is used in the response.

This is done before "ProcessRequest" which process the controller and ActionResult derived class.

I have made available my code.

http://www.k2em.com/content/common/K2ECSocialNetwork_110803.zip

It is base on the book https://www.packtpub.com/asp-net-4-social-networking/book 

Which i am trying to convert to MVC.

Password for the admin account is "password"

Error occurs in the "ExecuteResult.cs"  which gets call from the "K2ECProfileController.cs".

At this time i do not  know how to resolve this issue if

   var httpResponceBase = new MvcPlayerHttpResponseWrapper(new HttpResponse(responseWriter), pageId);
   var httpContext = new MvcPlayerHttpContextWrapper(Context, httpResponceBase);

 should be using a "responseWriter" that is not text base or i should be using a different image displaying method because core code should not be modified.

Thanks in advance for any help.

Minh

Developer
Aug 8, 2011 at 11:23 AM

Hi

Can't download you sources

But can you try get Response directly like this

 

/// <summary>
/// Summary description for BookImageResult
/// </summary>
public class BookImageResult: ActionResult
{

    public BookImageResult()
    {

    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = HttpContext.Current.Response;
        byte[] image =  C1File.ReadAllBytes(PathUtil.Resolve("~/Composite/images/about.png"));
        response.Clear();
        response.ContentType = "image/png";
        response.BinaryWrite(image);
        response.End();
    }

}

Developer
Aug 8, 2011 at 11:42 AM
Edited Aug 8, 2011 at 11:42 AM

For disabling threadAborterd error this code is better:

public override void ExecuteResult(ControllerContext context)
    {
        var page = HttpContext.Current.Handler as System.Web.UI.Page;
        page.PreRender += (a, b) =>
        {

            var response = HttpContext.Current.Response;
            byte[] image = C1File.ReadAllBytes(PathUtil.Resolve("~/Composite/images/about.png"));
            response.Clear();
            response.ContentType = "image/png";
            response.BinaryWrite(image);

            response.End();
        };
    }

Aug 8, 2011 at 2:50 PM

thats a big assumption to make, that you're using the standard asp.net rendering engine, and not some other like razor, or spark or NHAML

Aug 8, 2011 at 9:08 PM

Using Response directly worked.

Thanks

Minh

Aug 19, 2011 at 4:22 PM

Sorry i have the code on amazon server and have to turn it off during weekday when i am not working on it.

Still a struggling programmer and can't afford amazon fees yet.

Here is the latest code if any one is interested.

http://www.k2em.com/content/common/K2ECSocialNetwork_110819.zip

Minh