0

Situation

I have two applications that both read and save images / documents to one specific folder. Example if the user uploads their image in one program, a user of program B is supposed to view and even edit that image.

What I have done

In one of my application I have created a setting in settings for the string of my path. So to save an image path I call

 path = System.IO.Path.Combine(Properties.Settings.Default.imagePath, Fimage);

this works fine. However, my issue is when I try to view the image in my edit view.

Controller

 ViewBag.imagePath =   Properties.Settings.Default.imagePath;

View

 <img src="@ViewBag.imagePath/@Url.Content(@Model.image)" alt="Image" style="height: 255px;  " />

Problem

The problem is upon attempting to view the image I get the error, Not allowed to load local resource: I have full access to the folder and when I attempt to browse to the file in the error message the picture is displayed. I was advised from other questions to use, server.MapPath however when I do Server.MapPath(Properties.Settings.Default.imagePath); I get the error physical path received, expected virtual path.

This viewbag holds the entire string of folder my files are stored with the EVERYONE user having full access. So i'm really unsure of why it's making a fuse.

P.S I cant say something like "~/content/images" because the path to that file is in an entirely different application, I think I need to give it the entire location.

Any assistance will be appreciated.

Gratitude

Niana
  • 1,057
  • 2
  • 14
  • 42

1 Answers1

0

Looks like your defaultImage variable holds a physical path, which will work for saving the image since the API requires a path in the form e.g. c:\website\images\.... but for viewing, you need the VirtualPath, something of the form http://mycoolsite/images/image.jpg and this is what the error is telling you.

Your Controller; therefore, should return something like

ViewBag.imagePath = Properties.Settings.Default.imagePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty); 

In order to get the Relative Path. See this other related question.

EDIT I just saw your "PS..."

If that's the case, then you need to pass the full URL to the other application: (e.g. http://someothersite.com/images/image.jpg).

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Revering to your Edit, how do I state that in my config, im trying `http://someothersite/Manage/Content/Image/` and im getting is not a valid virtual path. – Niana Jul 06 '17 at 21:37