【问题标题】:Ashx file creating images in MVC ASP.netAshx 文件在 MVC ASP.net 中创建图像
【发布时间】:2010-12-18 19:11:31
【问题描述】:

我正在动态创建图像并且需要向用户展示这些图像。 为此,我创建了一个 ashx 文件,但问题是这个 ashx 文件永远不会被调用,不确定为什么路径问题或需要在 web.config 中添加任何标签。 虽然调试它不去..可能是它没有找到 请指教。

编辑:当我直接点击 ashx 网址时,它会显示一些结果

编辑 1:知道会话在上下文中为空是什么原因?

或 MVC asp.net 不需要 ashx 处理程序,请告知。

/// <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;
        }
    }
}

【问题讨论】:

    标签: asp.net-mvc httphandler


    【解决方案1】:

    在控制器中,您需要创建一个返回 FileResult 的函数

    在 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
    

    在视图中,只需要调用

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

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2018-09-04
      • 2011-01-10
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多