1

I store image in folder App_Data in project Web API. I stored in the database with the path:

 var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/"), user.Username + ext);

From project MVC client i call Web Api, from path image i cannot show tag <img />. Chrome show error: Not allowed to load local resource.

<img id="img_id"  width="70px" heigh="70px" src="D:\Nguyen Nhu\Lap Trinh Hien Dai\ServerAPI\ServerAPI\App_Data\nophoto.gif"/>

How to fix it?

Nhu Nguyen
  • 874
  • 3
  • 18
  • 33

1 Answers1

2

In general, you should use Url.Content and not Server.MapPath to map served client Urls in MVC. Server.MapPath returns the physical folder, and should be used only by server-side code.

However, App_Data is a special folder and files stored there won't be served by IIS.

TL;DR: You'll need to use another folder to store your images in, e.g.

Url.Content('~/MyImagesFolder/' + user.Username + ext);

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • I can't call Url.Content method not exist. – Nhu Nguyen Dec 18 '12 at 18:56
  • At a guess, you are creating the Url in a data layer, away from MVC, hence the error. Can I suggest that you store just the filename (e.g. `user.UserName + ext`) in your database, and then create the served image url when you render the page, using `Url.Content`. This will have several benefits, notably portability of file storage location. – StuartLC Dec 18 '12 at 19:06