views:

69

answers:

3

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
Hmmm. Do you know what the rationale is for requiring a rooted directory for this control? It seems crazy to me.
Tom V
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
Yeah. But in my case, the users don't have any control over where the file is saved. Only my program does.
Tom V
+4  A: 

Use the pages MapPath method:-

ctl.SaveAs(MapPath("~/UserFiles/[username]/[filename]"));
AnthonyWJones
Thanks. Everything makes sense now.
Tom V
ummm . . . is there a way for me to create "~/userdata/[username]" if it doesn't already exist?
Tom V
System.IO.Directory.CreateDirectory(MapPath("~/userdata/[username]"));
AnthonyWJones
A: 

Sometimes the MapPath is not directly accessible.

In this case use

ctl.SaveAs(Server.MapPath("~/UserFiles/[username]/[filename]"));
Eibx
is there a way for me to create "~/userdata/[username]" if it doesn't already exist?
Tom V