3

I need to display an image from my database. I post a form with some data with Json. After that, I get the result from the database. It's almost the same as a reaction in facebook. You post your comment and you will get your comment formatted and your profileimage on the side.

I managed to get the data back but I don't know how to get the image on the screen... Should I try to create and url? I tryed that but I don't know how to resolve the url in a javascript file.

Sorry, i'm new with Json and MVC 3.

Thanks

LockTar
  • 5,364
  • 3
  • 46
  • 72
  • 1
    possible duplicate of [Display image from database in asp mvc](http://stackoverflow.com/questions/880515/display-image-from-database-in-asp-mvc) – Jakub Konecki Aug 08 '11 at 21:49
  • I tryed this but i need to render some html tags (some table rows and cols) with an image in JavaScript. Now I dont know how to make the good url in JavaScript. The url must be relative so you can use it in any page you like... – LockTar Aug 08 '11 at 23:58
  • Just use Url.Action() to generate an url to the action that returns your image. – Jakub Konecki Aug 09 '11 at 08:06

4 Answers4

0

HttpHandler works well in displaying images either in image control or in GridView.

Sandy
  • 105
  • 7
0

JSON is not efficient at all for sending/receiving binary data like images. I would use FileResult. This reference will help you.

Can an ASP.NET MVC controller return an Image?

Community
  • 1
  • 1
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • Thank you for the response. I understand how to get a picture back on the screen. The thing is that I dont know how to render a good url. Say i have an imagecontroller with action getimage with profileid as parameter. Now i have the id in my json result in my JavaScript file. How do I render the proper url?? – LockTar Aug 08 '11 at 23:48
  • Normally you would use @url.Action or something like that. But now you are in a JavaScript file so you cant use it – LockTar Aug 08 '11 at 23:52
  • I am not sure I understood you well. Let's say your route is {controller}/{action}/{id} and profileid is 3. Then url of image must be, for example, "/Image/GetImage/3" . So everything is fixed except for id part. Why is this a problem in javascript? – Tae-Sung Shin Aug 09 '11 at 00:39
  • If the image is created on the fly and not stored in Hard Disk or DB, how should it be rendered from JSON result? – ghedas Dec 24 '11 at 12:46
0

I'm not sure I understand. Do you actually wish to retrieve the image in the JSON data request? I would simply return the URL for the image on your web-server and dynamically insert a 'img' with "src=URL-FROM-JSON" into your webpage via Javascript. There are a lot of functions in the JQuery library to help with this.

Nadir Muzaffar
  • 4,772
  • 2
  • 32
  • 48
  • Thank you for the response. I understand how to get a picture back on the screen. The thing is that I dont know how to render a good url. Say i have an imagecontroller with action getimage with profileid as parameter. Now i have the id in my json result in my JavaScript file. How do I render the proper url?? – LockTar Aug 08 '11 at 23:50
  • Normally you would use @url.Action or something like that. But now you are in a JavaScript file so you cant use it – LockTar Aug 08 '11 at 23:52
  • For a given profileId you would have to have the associated files somewhere on the web server with an absolute path, so I'm not sure what you mean by generating a good URL? For example, it seems that facebook puts all profile pictures under the static link https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/profilePictureID – Nadir Muzaffar Aug 09 '11 at 00:29
0

It is a bit stupid... I forgot that you could just use the controller and then the action. I did ImageController and not Image so the action wasn't found. Thats why I thought that this wouldn't worked. I now make an url property on the server and send that with Json. That property is rendered in the src attribute of the image tag.

LockTar
  • 5,364
  • 3
  • 46
  • 72