Ashx file creating images in MVC ASP.net

拈花ヽ惹草 提交于 2019-12-11 15:45:50

问题


I am dynamically creating images and need to show those images to users. For that i have created a ashx file but the problem is this ashx file is never getting called not sure why path issue or need add any tags in web.config .. while debugging its not going .. might be its not finding please advise.

EDIT: When i directly hit the ashx url its going and showing some results

EDIT 1: Got to know that session is null in the context any reason ?

or MVC asp.net don't require ashx handlers please advise.

/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        Log.Info(" In theGetImage");
        context.Response.Clear();
        byte[] imageByteArray = System.Convert.FromBase64String(context.Session["FrontJpegBase64"].ToString().Replace(' ', '+'));
        // System.IO.MemoryStream imageMemoryStream = new System.IO.MemoryStream(imageByteArray);

        try
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageByteArray)))
            {
                img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

        }
        catch (System.Exception ex)
        {
                       }
        finally
        {
            // img.Close();
            context.Response.Flush();
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

回答1:


in the controller, you need to create a function that return a FileResult

in vb.net

Function img() As FileResult
    Dim bmp As Bitmap = Nothing
    Dim dll As New Chess.cChessBoard

    dll.drawBoardPNG(bmp)

    Dim imgStream As New IO.MemoryStream
    bmp.Save(imgStream, ImageFormat.Png)
    imgStream.Position = 0

    Return File(imgStream.ToArray, "image/png")
End Function

in the view, you only need to call

 <img src="/test/chess/img" />


来源:https://stackoverflow.com/questions/1771585/ashx-file-creating-images-in-mvc-asp-net

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