creating a uniquely named 'FolderName' from the code behind for 'this' pages uploads

Dec 4, 2008 at 3:46 AM
hello flajaxian,

i've been testing out setting the 'FolderName' from the code behind: the idea being to create a uniquely named folder when the page loads for all the files uploaded by this particular page.

this is what i have so far:

protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //create a guid for the folder name
        string strGuid = System.Guid.NewGuid().ToString().Replace("-", string.Empty);
        hfGuid.Value = strGuid;

        //create the folder in the uploads folder
        clsFileFunctions.CreateDirectory("processing\\" + strGuid);

        //pass the folder name to the upload control
        ((com.flajaxian.FileSaverAdapter)FileUploader1.Adapters[0]).FolderName = "~\\_uploads\\processing\\" + strGuid;
    }
}

this currently creates a folder when the page loads (which is good) but then also creates one folder per file that is uploaded and then places that file in that folder (which isn't so good!).

is it possible to do this, i.e. create one folder and upload all the images into it?

any advice would be grand!

cheers,

jake
Coordinator
Dec 4, 2008 at 4:06 AM
protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack && !FileUploader1.FileIsPosted)
    {
        ...
    }
}
Dec 4, 2008 at 4:42 AM
thank you for your swift response!

given that a go:

protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack && !FileUploader1.FileIsPosted)
    {
        //create a guid for the folder name
        string strGuid = System.Guid.NewGuid().ToString().Replace("-", string.Empty);
        hfGuid.Value = strGuid;

        //create the folder in the uploads folder
        clsFileFunctions.CreateDirectory("processing\\" + strGuid);

        //pass the folder name to the upload control
        ((com.flajaxian.FileSaverAdapter)FileUploader1.Adapters[0]).FolderName = "~\\_uploads\\processing\\" + strGuid;
    }
}

the folder gets created, but the files don't get uploaded to the folder...

should i be passing the folder name outside of the if statement?

cheers,

jake


Coordinator
Dec 4, 2008 at 6:34 AM
protected void Page_Init(object sender, EventArgs e)
{
    if (!FileUploader1.FileIsPosted)
    {
        //create a guid for the folder name
        string strGuid = System.Guid.NewGuid().ToString().Replace("-", string.Empty);
        hfGuid.Value = strGuid;

        //create the folder in the uploads folder
        clsFileFunctions.CreateDirectory("processing\\" + strGuid);

        FileUploader1.RequestAsPostBack = true;
    }else{
        //pass the folder name to the upload control
        ((com.flajaxian.FileSaverAdapter)FileUploader1.Adapters[0]).FolderName = "~\\_uploads\\processing\\" + hfGuid.Value;
    }
}
Coordinator
Dec 4, 2008 at 4:04 PM
But be careful the solution you have right now would allow a malevolent user to use denial of service, by simulating a huge number of requests and creating an infinite number of folders. You probably want to tie somehow the name of that folder to the username.
Dec 4, 2008 at 10:46 PM
Edited Dec 4, 2008 at 11:22 PM
yup, that shouldn't be a problem as this uploader would only be available to logged in users. although, that's not to say a logged in user isn't malicious... ;)

unfortunately, the code above solves one bit in that it only creates one folder, but the files get uploaded to the "~\\_uploads\\processing\\" folder, not the guid folder!

i'm wondering if it'd be better off uploading the files and making sure they have unique names, storing the names in a hidden filed and then processing them once the page is submitted and the other form data added to the database?

thanks for your help,

jake


Coordinator
Dec 4, 2008 at 11:43 PM
If it is so then hfGuid is not a hidden field or it is set to have EnableViewState="false" otherwise it would work.

Usually the folder should have the unique name or ID spacific for the user then you can append a unique index before the name of each image to resolve name conflicts.