The FileUpload control requires me to provide a rooted directory in the SaveAs() method. However, I would much rather the uploaded files go into a virtual directory, such as "~/UserFiles/[username]/[filename]". Is there a way for me to accomplish that?
A:
No, the keyword is requires. You can use your script to copy the file after the download.
gbrandt
2009-03-06 07:28:37
Hmmm. Do you know what the rationale is for requiring a rooted directory for this control? It seems crazy to me.
Tom V
2009-03-06 07:32:14
The rational is that its a physical file path since you are saving to the file system. Paths in URLs are logical paths not physical. Quite likely many uses will save the file to somewhere outside of the folders representing the website.
AnthonyWJones
2009-03-06 07:35:56
Yeah. But in my case, the users don't have any control over where the file is saved. Only my program does.
Tom V
2009-03-06 07:40:38
+4
A:
Use the pages MapPath method:-
ctl.SaveAs(MapPath("~/UserFiles/[username]/[filename]"));
AnthonyWJones
2009-03-06 07:33:23
ummm . . . is there a way for me to create "~/userdata/[username]" if it doesn't already exist?
Tom V
2009-03-06 07:55:40
System.IO.Directory.CreateDirectory(MapPath("~/userdata/[username]"));
AnthonyWJones
2009-03-06 08:23:32