Search code examples
c#asp.netajaxflajaxianhttpfilecollection

Flajaxian Custom Folder Save Location


I see a lot of people coming up with some excessive ways to change the folder location on the fly with flajaxian multiple file upload control.

Was just wondering if the more experienced could take a look at the way I've come up with and let me know if there are any major issues I should be concerned about. (Assuming I have the proper error checking in place.)

I planned on initializing the control as seen below. :

<cc1:FileUploader ID="FileUploader1" runat="server" OnFileReceived="fileUploader_FileReceived" RequestAsPostBack="true">

    </cc1:FileUploader>

(I RequestAsPostBack="true" as there are some other controls I need to check in my event handler)

I simply change the HttpFileCollection.SaveAs property in the fileUploader_FileReceived event. Since flajaxian does this one file upload at a time, we can expect that there is only 1 file in the collection (or else we could use a loop).

protected void fileUploader_FileReceived(object sender, 
com.flajaxian.FileReceivedEventArgs e)
 {

        HttpFileCollection files = Request.Files;
        // Change path to whichever folder I need
        String TempFileName = "C:\\NEW\\PATH\\TO\\Folder\\" + files[0].FileName;
        // Save the file.
        files[0].SaveAs(TempFileName);
}

This implementation seems to work great as long as the folder is existing! I was just wondering if there is anything technically wrong with an implementation like this, again , assuming all error checking was in place.

Thanks!


Solution

  • A better way to do this would be to use an adapter, and over write the folder location in the OnFileNameDetermining event. This way, we also get all the goodies with the adapter.

    <cc1:FileUploader ID="FileUploader1" runat="server"` OnFileReceived="fileUploader_FileReceived" RequestAsPostBack="true">
                <Adapters>
                    <cc1:FileSaverAdapter runat="server" FolderName="Ups" OnFileNameDetermining="fileUploader_FileDetermined" />
                </Adapters>
    </cc1:FileUploader>
    

    In the file determined event, we can change the folder location programatically

    protected void fileUploader_FileDetermined(object sender, com.flajaxian.FileNameDeterminingEventArgs e)
    {
        e.FileName = "C:\\NewFolder\\" + e.File.FileName;
    }
    

    We can use the FileReceived event to check if the folder exists, and if not, create it.

    protected void fileUploader_FileReceived(object sender, com.flajaxian.FileReceivedEventArgs e)
    {
         int fileIndex = e.Index;
         if (fileIndex == 0)
         {
            // We are on our first file, check if the new folder exists, if not, create it
         }
    }