How do I add a FileUploader to my site without hard coding the path for SaveAs?

bmanmike39
bmanmike39 used Ask the Experts™
on
This is what I'm doing now, and it works.  But to load it on a server the path can’t be hard coded.  How would I do this

My code:

FileUpload1.SaveAs("C:\\Inetpub\\the rest of the path\\awiRw\\images\\" + Label1.Text + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".")));

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Make sure the permissions are set appropriately and try this code:
http://naspinski.net/post/Improved-SaveAs()-Method-on-the-FileUpload-Control.aspx

Author

Commented:
I don’t understand this.
Top Expert 2013

Commented:
Use server to MapPath
string path = Server.MapPath(@"~/images");
FileUpload1.SaveAs(path+ Label1.Text + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".")));
   
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Top Expert 2013
Commented:
my bad I meant Use Server.MapPath()
http://www.4guysfromrolla.com/webtech/121799-1.shtml

Author

Commented:
Where do I put this string path = Server.MapPath(@"~/images"); so all my pages can use this.  I have 5 page with a file uploader.
Top Expert 2013

Commented:
If that is going to remain same for all FileUpload control you can store the value in web.config like:

<appSettings>
      <add key="folderPath" value="~/images" />
  </appSettings>

//get the value from web.config like: ConfigurationSettings.AppSettings["ConnectionInfo"];
string path = Server.MapPath(ConfigurationSettings.AppSettings["ConnectionInfo"]);
FileUpload1.SaveAs(path+ Label1.Text + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".")));

e.g.:http://odetocode.com/Articles/345.aspx
Top Expert 2013

Commented:
my bad again:
//get the value from web.config like: ConfigurationSettings.AppSettings["folderPath"];
string path = Server.MapPath(ConfigurationSettings.AppSettings["folderPath"]);
Top Expert 2013

Commented:
Using Server.MapPath is the answer to bmanmike39's original question.
Storing path in web.config is one of the possible ways for his follow-up question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial