0

I have a FileLoad controller as such:

     <asp:FileUpload ID="FileUpload1" runat="server" />

What I like to do is to upload the file to the /images/logos folder on the server once the user selects the gif image:

     string uploadFolder = Server.MapPath("/images/logos/");

     string uploadfile = uploadFolder + "Image1" + ".gif";
     FileUpload1.SaveAs(uploadfile); 

When I look at the value of uploadfile it starts with C:....

I believe it should be something like /images/logos/Image1.gif as when it completes, nothing is in the images/logos folder. Note that I do not get any errors.

What am I doing wrong.

1
  • That is weird man. Do you have this virtual folder set up in IIS? May be you could put a break point at FileUpload1.SaveAs(uploadfile); and check what the uploadfile path is? Commented Nov 19, 2012 at 21:22

2 Answers 2

1
Server.MapPath("/images/logos/") // Will map to the wwwroot folder
Server.MapPath("~/images/logos/") // Will map to the application folder
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jack. Is server.MapPath what I should user. As mentioned, when I test it locally, it give C:\\....
@WebDev: I would recommend using it. It's the easiest way to get either the relative ("/images") or virtual ("~/images") folder. Just remember, "/images/logos" will be a folder off of your IIS site whereas "~/images/logos" will be within your application. When you deploy to your production environment, if the wwwroot folder is on the D: drive it will map it accordingly. HTH
0

Server.MapPath gives you the absolute path on the server based on the relative path you provide.

http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx

2 Comments

Thanks. If I am using the fileupload, is this what I need though? Server.MapPath?
That's one way to do it, I tend to shy away from using Server.MapPath because it's harder to test. I usually use a configuration entry for a base directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.