0

I have setup a network path on the web server running the .net MVC application to another server that is being used for storage of files that are uploaded. In order to allow for uploading files to the network path through the application I amended the application pool in IIS so that the application had permission to upload to it. This all works fine.

In order for the app to read the file when loading a page I created a virtual directory in IIS which appears now as part of the file system for that site in IIS. How can I then access and generate a path for this location in my app so that the path can be passed to a DB and then the image loaded later on a page?

Let's say my path is:

virtual path is useruploads/image.jpg

How can I have my application recognise the virtual path I've created?

Alternatively scrapping the virtual directory in IIS, is there a way I can have my application access the network path on the web server directly?

edit - To add to the above I have the network path mapped in iis and it shows under my site in iis as follows:

enter image description here

I would like to then read from this directory inside the asp.net mvc app using an address like this ~/useruploads/file.jpg. If I can get my app to recognise this virtual directory then I will be able to read from it.

edit no.2

Ok I think I have determined the nature of my issue and am a lot clearer. Basically I am accessing the file just fine when I link to it on the webpage. However I get the message 'not allowed to load local resource' in chrome. I believe this is simply a question of web browsers not allowing me to load the image because it is referenced on the page with a local address (the network path). As such I am now trying to create an action in the application to load the image with Url.Action directed at my action which will then convert the id into a stream of the image e.g.

In my view:

<img src="@Url.Action("image", "files", new { image = Model.image})" alt="name" />

Where Mode.image is something like folder\image.jpg.

Action:

public FileResult image(string id)
        {
            var dir = @"\\SERVERNAME\upload\";
            var path = Path.Combine(dir, id);

            return base.File(path, "image/jpg");
        }
Rob
  • 199
  • 21
  • "In order for the app to read the file when loading a page I created a virtual directory in IIS which appears now as part of the file system for that site in IIS" - can you explain this; why are you creating a Virtual Directory for a file storage area ? Are you having an issue reading a file from within your ASP.net MVC application from the Web HDD or another servers HDD ? – PhillipH Jul 15 '17 at 13:20
  • The network path on the web server is from another server, so another servers HDD as a network path on the web server. I am able to upload to it using the address \\serverIP\useruploads however I can't seem to read from it using the same address path. I need a path that the app recognises, hence trying to add the virtual directory in iis for the app and then asking here to see if there was a way my app could 'see' the virtual directory e.g. ~/useruploads/file.jpg so I can reference it like this in the app – Rob Jul 15 '17 at 13:31
  • I should say, when I say 'read from it' I mean load the image on a html page using that link through the app. So the app calls \\serverIP\useruploads\image.jpg as the address for the image from the db but this gives an 'not allowed to load local resource' error in chrome and doesn't load in any other browser. What I need really is for that network path to be mapped to a virtual address the app can recognise, I think at least. – Rob Jul 15 '17 at 13:40
  • Adding a new VD isn't the way to do - you need to be able to read the UNC path of the other server using the Application Pool identity user's account of your ASP.net MVC application. This is a very common scenario - you just need to allocate the correct permissions. – PhillipH Jul 15 '17 at 14:14
  • Fair enough. I do have the rights to read that folder using the account set in my application pool, I had to set this up to be able to upload to the folder. Double checking now the folder I can see the account I am using has full control rights over the folder read/write etc. So permissions don't seem to be the problem. – Rob Jul 15 '17 at 14:20
  • use server.MapPath() to map relative url to physical paths in your application. – LogicalDesk Jul 15 '17 at 16:41
  • Hello Codeunderflow, I'm not sure how that helps, my issue is that the location is not a physical path in my application, it is a physical path on the web server the application is hosted on. I can reference that physical location just fine but because it is a local path on the server it can't be loaded in the browser. So I need to either fake it's location as part of the application somehow or do what I think I've just worked out which is to send an id to a controller action in my app, which gets the file from the network path and streams the image to the page. – Rob Jul 15 '17 at 16:48
  • @Rob I suggest you put some debugging code around this (1) can you enumerate the files in the UNC drive ? (2) Is the file you are looking for in the enumeration ? (3) When you attempt to open the file, read-only, do you get an error, and what is the exact error code (ie. DOS Error 2, 5 etc). How have you ascertained that your app pool account has the correct rights and can see the file ? Normally app pool accounts are 'non interactive' and you cannot login using those credentials. – PhillipH Jul 15 '17 at 17:13
  • Hello Phillip, I've updated my question as I think I've determined what my issue is, it's simply that I can't load the image using a local address even if I have access to that address as browsers will not allow it. The address must be https://something. Thus I am attempting what I have stated in my question, if you can offer any assistance it would be much appreciated. – Rob Jul 15 '17 at 17:34

1 Answers1

0

So in the end my issue was that I was unable to place the image on the page as it was using a local address on the server which is not allowed for security reasons in browsers. In order to use the network path I had to use

<img src="@Url.Action("image", "files", new { image = Model.image})" alt="name" />

in my view to this action:

public FileResult image(string id)
    {
        var dir = @"\\SERVERNAME\upload\";
        var path = Path.Combine(dir, id);
        string contentType = MimeMapping.GetMimeMapping(path);

        return base.File(path, contentType);
    }

This then returns the image to the page although it means the image does not get cached.

Rob
  • 199
  • 21