Question

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!

Was it helpful?

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
     }
}

OTHER TIPS

What you are doing is fine, although, if you are saving files within the web site, consider using the MapPath method to create a physical folder from a virtual path within the web site

MapPath("/Images/User1")

This my mininal APSX implementation

<fjx:FileUploader ID="FileUploader1" runat="server" OnFileReceived="FileUploader2_FileReceived">
</fjx:FileUploader>

No adapters or folder is specified. When the FileRecevied event fires, I save files to a folder based on the Forms Authentication user name (names do not use characters not allowed in folder names).

Also note that the FileReceivedEventArgs has a reference to the (HTTP) file

e.File

The FileUploader control will show all files processed - you can even set the status code (e.g. 550) if there is an error, which is returned to the client.

Note that, the server call to the FileReceived event does not occur inside a nornal page postback, even if you specify

RequestAsPostBack="true"

So, a PagePreRender does not take place.

The only issue is, how do you perform any other processing at the client after the uploads complete (e.g. showing images uploaded).

Work I have in progress to this end is to use the client side event

FileStateChanged

When the last file is processed

if (file.state > Flajaxian.File_Uploading && isLast) {

I use JQuery to click a hidden submit button. The postback looks through session values stored when the files were saved, and renders back the images into a DIV.

However, an immediate submit causes issues with empty session inside the FileReceived event for some reason (I assume because the internal asynchronous call back has not completed). A pause of a few seconds before initiating the postback works OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top