1
$.ajax({
                    type: 'GET',
                    dataType: 'image/jpeg',
                    url: '/Home/GetImage/' + img , 
                    success: function (data) {
                        i = new Image();
                        i.src = data;
                        $('#imageresult').append(i);
                    },
                    error: function () {
                        alert('error'); 
                    },
});


[HttpGet]
public ActionResult GetImage(string img)
{
   string imageFile = System.Web.HttpContext.Current.Server.MapPath("~/Profile/Small/" );
   var path = Path.Combine(imageFile, img );  
   var srcImage = Image.FromFile(path);
   var stream = new MemoryStream();
   srcImage.Save(stream, ImageFormat.Jpeg);

   return File(stream.ToArray(), "image/jpeg");
}

It always falls into error function. If i remove dataType, then it doesn't fall into error function but it just shows long strings in the view. Why doesn't that work and show image in view?

6
  • 1
    you aren't sending any data then why POST? Commented Nov 1, 2016 at 9:42
  • Why aren't you using GET instead of POST? Agre, if you are using POST i think you need send some data. Commented Nov 1, 2016 at 9:45
  • ok i changed it as 'GET' Commented Nov 1, 2016 at 9:51
  • Try to send only bytearray then get the image in view. return Json(stream.ToArray()); Commented Nov 1, 2016 at 9:51
  • I think the wrong here is on your attribute on controller is HttpPost,but your ajax call type is GET.One more thing why dataType: 'image/jpeg',you are passing a string only Commented Nov 1, 2016 at 9:52

2 Answers 2

1

You can do something Like this:

public ActionResult GetImage(string img)
{
   string imageFile = System.Web.HttpContext.Current.Server.MapPath("~/Profile/Small/" );
   var path = Path.Combine(imageFile, img );  
   var srcImage = Image.FromFile(path);
   var stream = new MemoryStream();
   srcImage.Save(stream, ImageFormat.Jpeg);

   return Json(Convert.ToBase64String(stream.ToArray()), JsonRequestBehavior.AllowGet);
}

and the Ajax call:

$.ajax({
        type: 'GET',
        url: '@Url.Action("GetImage", "Home")',
        success: function (data) {
            $("#ItemPreview").attr('src', 'data:image/png;base64,' + data);
        },
        error: function () {
            alert('error');
        },
    });

and the view:

<img id="ItemPreview" src="" />

of course you can append img dynamically, then bind its source after.

Sign up to request clarification or add additional context in comments.

Comments

0

You are confusing the URL of an image with its actual content (the graphic bytes).

The /GetImage call returns the bytes, but you can never put data bytes in the i.src attribute because that attribute needs to be fed a URL.
It might work if you forget about the Ajax call and just use i.src = '/Home/GetImage/' + img;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.