-1

I know that similar questions have been asked A LOT but none of the answers have been any help to me yet. Pretty much, I am working with an MVC project and a CL project someone else created from a former desktop app that was actually supposed to be a web app (long and frustrating story). He made it so an image generated programatically is stored on the User's C:/ drive and then later displayed, which works fine in a desktop app, but not so much in a web app.

I have tried the <img src="file:/filepath">, but no luck. I know that I wouldn't be able to get to a file on a user's C:/ drive directly and should upload the image to the server and then go from there, but I have no idea how to go about that, and was hoping that someone could point me in the right direction.

trcoats
  • 23
  • 2
  • Have you tried converting the image into data and displaying it through src="data:image..."? – CrudaLilium Apr 01 '17 at 17:50
  • Could you show some of the other answers you have tried and explain why they were not of any help? I worry that anything we say would also not help. – Dour High Arch Apr 01 '17 at 17:55
  • @DourHighArch [link](http://stackoverflow.com/questions/4090712/why-cant-i-do-img-src-c-localfile-jpg) Here's one of a few that I found. Most of them just say "it's a security vulnerability" and stop at that, or say to use file:/ before file location. – trcoats Apr 01 '17 at 18:03
  • I also should mention that most of the the threads that I looked at were mostly for just beginner's HTML. It looks like the data:image route would definitely be the way to go as mentioned by @CrudaLilium and Cristian's answer. – trcoats Apr 01 '17 at 18:20
  • @CrudaLilium, doing it that way did the trick! I'm sorry I couldn't give you a marked answer or anything, but you were the first one to suggest it. Thanks all! – trcoats Apr 01 '17 at 19:04
  • No problem, glad I helped. – CrudaLilium Apr 29 '17 at 11:18

1 Answers1

1

Assuming you store your image as:

public byte[] MyImage { get; set; }

You can pass it to the view:

public ActionResult Index()
{
    ViewBag.MyImage = Convert.ToBase64String(MyImage);
    return View();
}

In your view display it like:

<img src="data:image/png;base64,@ViewBag.MyImage" />
Cristian Szpisjak
  • 2,429
  • 2
  • 19
  • 32