twal
twal

Reputation: 7039

Providing the path to an image stored on the server, not in the application files

I need to display images on my ASP.NET MVC page that will be stored on the server i have an apphelper class that I can use to provide the path

like this

public static class AppHelper
{
     public static string ImageLowResPath(string imageName)
     {

      }
}

How can I get the file path that is stored on the c: drive of the server here?

In my view I will get the filepath like this

img src='<%=AppHelper.ImagelowResPath("10-1010.jpg") %>'

Thank you

Upvotes: 2

Views: 783

Answers (3)

marc.d
marc.d

Reputation: 3844

you have to create a action that returns a FileStreamResult if the file is outside of your wwwroot.

eg.

    public FilestreamResult GetPicture(string Filename) { 
        Filename = @"C:\SomePath\" + Filename;
            return new FileStreamResult(new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read), "image/jpeg"));
    }

your html should now look like this

<img src="/Controller/GetPicture?Filename=test.jpg" />

Update as long as your images are static content which do not change frequently and you dont have the need to implement some kind of access control this is indeed not the best solution.

under terms of best practice you should split your components accross multiple domains. yahoo has published a excellent guide about best practices for speeding up websites http://developer.yahoo.com/performance/rules.html#split

Upvotes: 2

FlyingStreudel
FlyingStreudel

Reputation: 4464

Uh I dont know exactly what you are asking for but you could try to impersonate and then access the server share?

[DllImport("advapi32.dll",EntryPoint = "LogonUser", SetLastError = true)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

IntPtr admin_token = IntPtr.Zero;
WindowsIdentity wid = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin;  
WindowsImpersonationContext wic;

LogonUser(user, servername, pass, 9, 0, ref admin_token)
wid_admin = new WindowsIdentity(admin_token);
wic = wid_admin.Impersonate();

Once you've impersonated someone with appropriate priveleges you could go to

\\servername\c$\(image path)

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61589

You need to use Server.MapPath(...) which will map a virtual path to its physical location on disk:

string path = "~/Images/10-1010.jpg";
string filePath = Server.MapPath(path);

Upvotes: 0

Related Questions