简体   繁体   English

如何使用Ajax更新ASP.NET MVC Controller中作为文件返回的图像?

[英]How to update image returned as File in ASP.NET MVC Controller using Ajax?

I have a base image, and I use C# Graphics library to add data to it, and then return the image converted to byte array. 我有一个基本图像,我使用C#图形库向其中添加数据,然后将图像转换为字节数组。

I have a controller: 我有一个控制器:

    public ActionResult DisplayDR(int drNum)
    {
        ER dr = DataUtility.getDR(drNum, errors);
        return File(DrawDRUtility.getDRimageAsByteArray(dr), "image/png");
    }

That works fine with, 很好用,

<img id="drImage" src="@Url.Action("DisplayDR")" width="1110"  height="484" alt="qr code" />

I need to be able to dynamically update the image and attempted with ajax with no luck. 我需要能够动态更新图像并尝试使用Ajax,但没有运气。

Using: function getNewDRImage(instance) { 使用:function getNewDRImage(instance){

  var drNum = $(instance).data('val');
  var src = '@Url.Action("DisplayDR", "Home", new { area = "Part" })';
  $.ajax({
      type: "GET",
      url: src,
      data: { drNum : drNum },
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      error: function (err) {
          var msg = "Client side error, " + err;
          throwClientSideErrorMsg(msg)
      },
      success: function (data) {
         $('#drImage').attr("src", data.image);
      }
  });

} }

and returning JSON like so, 然后像这样返回JSON,

    public JsonResult DisplayDMR(int drNum)
    {
        ER dr = DataUtility.getDR(drNum, errors);
        return
            Json(new { image = File(DrawDRUtility.getDRimageAsByteArray(dr), "image/png") }, JsonRequestBehavior.AllowGet);
    }

Gives me this error: 给我这个错误:

System.Web.HttpException (0x80004005): The controller for path '/[object Object]' was not found or does not implement IController. System.Web.HttpException(0x80004005):找不到路径'/ [object Object]'的控制器或未实现IController。

What am I not doing correctly? 我做错了什么?

update, viewing the source after the ajax call I see why the error occurs, as this is what the response is: 更新,查看ajax调用后的源,我知道为什么会发生错误,因为这是响应:

<img id="dmrImage" src="[object Object]" alt="qr code" height="484" width="1110">

So I guess I am confused on what to do with the JSON data to update the image. 所以我想我对如何使用JSON数据更新图像感到困惑。

With your ajax code you're trying to load actual bytes of your image and assign that to the src attribute. 使用您的ajax代码,您试图加载图像的实际字节并将其分配给src属性。 That's not what the attribute expects. 这不是该属性所期望的。

All you need is to just assign a new image url as a string. 您只需要分配一个新的图像URL作为字符串即可。 The browser then will automatically fetch actual bytes and show the new image for you. 然后,浏览器将自动获取实际字节并为您显示新图像。

If you want to use the same url for the updated image you need to make sure the browser won't use your old cached image. 如果要对更新的图像使用相同的url,则需要确保浏览器不会使用旧的缓存图像。 Take a look at this answer for how to do this. 看看这个答案如何做。

To add Cache-Control: header to your File() response use Response.AddHeader() method: 要将Cache-Control:标头添加到File()响应中,请使用Response.AddHeader()方法:

public ActionResult DisplayDR(int drNum)
{
    Response.AddHeader("Cache-Control", "no-store");
    ER dr = DataUtility.getDR(drNum, errors);
    return File(DrawDRUtility.getDRimageAsByteArray(dr), "image/png");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM