views:

81

answers:

3

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

A: 

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);
Matthew Abbott
The images wont be saved with in the application. Just on the servers file system as they will be added outside the application. Will this still find them? They are not in the www root directory
twal
No, this will not find the images, unless they are mapped to another virtual directory.
SWeko
Ok I didn't think so, is there a way to get them off the servers file system then??
twal
A: 

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)

FlyingStreudel
Oh my bad, you are looking to find the filepath, not access the file.
FlyingStreudel
+1  A: 

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

marc.d
Oh my friend THANK YOU!! worked perfectly. I started doing something similar but was attempting to return it as a byte[] I Did not know about the FileStreamResult. This make life so much easier. Thank You!
twal
Now I am wondering if what I am doing to the best practice. I don't want to save all the images in my application as the folder containing the images is used my a few other applications to both read and write.Is there a better way get and upload these images like using a virtual directory(though i don't really know how to do this) or something?
twal