## Aurigma Image Uploader Flash 1.0

# Saving Uploaded Files in ASP.NET

- Collapse All Sections
- See Also
- Language Filter ( all languages )

Flash applications are not allowed to send files created by themselves as standard POST request fields accessible through the Request.Files collection. Image Uploader Flash bypasses this limitation to upload files. However, it causes some problems with retrieving these files on the server side. Image Uploader Flash ASP.NET control which comes with Image Uploader Flash provides several facilities to solve this problem. The present article describes these approaches in detail.

## Using Uploader Control

Note

If you use IIS 7.5 you should disable request validation on the page which hosts the ImageUploaderFlash control.

<%@ Page Language="C#" ValidateRequest="false" %>

This approach implies using the ImageUploaderFlash control to embed Image Uploader Flash into a page as it is described in the Quick Start with ASP.NET Control topic.

To handle the uploaded data this control exposes the FileUploaded and AllFilesUploaded events. The first one fires whenever user-specified file (along with thumbnails or icons prepared client-side depending on Image Uploader Flash configuration) and metadata extracted from it are successfully uploaded. It provides access to these data via parameter of FileUploadEventArgs type. The second event fires when the upload session completes and all the files and metadata are received on the server side. All these files are stored in a temporary directory and are accessible through the collection of FileUploadEventArgs objects.

To add FileUploaded and/or AllFilesUploaded event handler just specify its name in the control events list or add OnFileUploaded and OnAllFilesUploaded parameters declaratively:

### ASP.NET

    <aur:ImageUploaderFlash ID="Uploader1" runat="server"
        OnFileUploaded="FileUploaded"
        OnAllFilesUploaded="AllFilesUploaded">
    </aur:ImageUploaderFlash>

The page code-behind should contain these event handlers with the specified names:

### C#

    protected void FileUploaded(object sender, Aurigma.Uploader.FileUploadEventArgs e)
    {
        // Your code
    }

    protected void AllFilesUploaded(object sender, Aurigma.Uploader.AllFilesUploadEventArgs e)
    {
        // Your code
    }

All the data available through the FileUploadEventArgs properties can be divided into the following groups:

### Converted Files

This group consists of the ConvertedFiles property which returns a collection of converted files. A converted file represents original user-selected file or file created by Image Uploader Flash depending on its configuration (thumbnail, icon, or ZIP compressed file). Converted files are created and sent for each Converter added to the ImageUploaderFlash.Converters collection. If you are not familiar with converters read the Configuring Upload Settings topic first.

Each converted file is represented with the FileInfo class which contains the converted file and provides access to some additional information. To save a converted file you can use the SaveAs(String) method which saves this file to the specified path on a server.

### User-provided Data

This group includes the Angle and Description properties which contain the rotation angle and user-provided description respectively.

### Upload Details

This group consists of upload session-related properties. All the files and metadata related to a particular user-selected file are uploaded in a separate package (request). It means that the upload session contains as many packages as there are files in the upload list.

- PackageCount contains total number of packages in the current upload session.
- PackageGuid contains a unique identifier of the current upload session.
- PackageIndex contains index of the current package inside the upload session.

### Original File Information

This group includes properties containing information about the original file.

- SourceCreatedDateTime and SourceCreatedDateTimeLocal represent creation date of the original file.
- SourceLastModifiedDateTime and SourceLastModifiedDateTimeLocal represent last modification date of the original file.
- SourceHeight and SourceWidth represent dimensions of the original file (in pixels) if it is an image.
- SourceXResolution and SourceYResolution represent horizontal and vertical resolutions of the original file (in dpi) if it is an image.
- SourceName represent the original file name.
- SourceSize represent the original file size.

The code sample below demonstrates how to handle the FileUploaded event to save a source file and its resized copy to the ../Gallery/ and ../Gallery/Thumbnails/ folders respectively, and write some file-related information (filenames, image dimensions, and user description) to a XML file.

### C#

    protected void FileUploaded(object sender, FileUploadEventArgs e)
    {
        string galleryPath = "Gallery/";

        //Save original file.
        //It is the first file in ConvertedFiles collection as we set first converter mode to SourceFile.
        string physGalleryPath = Server.MapPath(galleryPath);
        string sourceFileName = GetSafeFileName(physGalleryPath, e.SourceName);
        e.ConvertedFiles[0].SaveAs(System.IO.Path.Combine(physGalleryPath, sourceFileName));

        //Save thumbnail.
        //It is the second file in ConvertedFiles collection as we set second converter mode to Thumbnail.
        string physThumbnailsPath = Server.MapPath(galleryPath + "Thumbnails/");
        string thumbnailFileName = GetSafeFileName(physThumbnailsPath, e.SourceName);
        e.ConvertedFiles[1].SaveAs(System.IO.Path.Combine(physThumbnailsPath, thumbnailFileName));

        //Save file info.
        XmlDocument descriptions = new XmlDocument();
        descriptions.Load(Server.MapPath(galleryPath + "Descriptions.xml"));

        XmlElement file = descriptions.CreateElement("file");
        file.SetAttribute("name", sourceFileName);
        file.SetAttribute("thumbName", thumbnailFileName);
        file.SetAttribute("width", Convert.ToString(e.SourceWidth));
        file.SetAttribute("height", Convert.ToString(e.SourceHeight));
        file.SetAttribute("description", e.Description);

        descriptions.DocumentElement.AppendChild(file);

        //Save xml file with uploaded files info.
        descriptions.Save(Server.MapPath(galleryPath + "Descriptions.xml"));
    }

    private string GetSafeFileName(string path, string fileName)
    {
        // ...
    }

Note

The upload script should return non-empty response.

## Using UploadRequest Control

Note

If you use IIS 7.5 you should disable request validation on the page which hosts the UploadRequest control.

<%@ Page Language="C#" ValidateRequest="false" %>

If using the ImageUploaderFlash control is inconvenient for you or you need to process uploaded data on another page (Image Uploader Flash is not hosted on), consider an alternative UploadRequest control.

This control allows processing uploaded data in the same way as the ImageUploaderFlash control. Just insert it into the page specified with the UploadSettings.ActionUrl property and add FileUploaded and/or AllFilesUploaded events handlers.

### ASP.NET

    <aur:UploadRequest ID="UploadRequest1" runat="server"
        OnFileUploaded="FileUploaded"
        OnAllFilesUploaded="AllFilesUploaded">
    </aur:UploadRequest>

Then handle them in the same manner as analogue events described above.

## Autosave

Both the ImageUploaderFlash and UploadRequest controls support the autosave feature which allows you to handle the upload without writing a line of code. All you need is just to set the AutoSave property to true. Then specify the folder where you want to save files via the DestinationFolder property.

### ASP.NET

    <aur:ImageUploaderFlash ID="Uploader1" runat="server"
        AutoSave="true"
        DestinationFolder="~/Gallery">
    </aur:ImageUploaderFlash>

If the specified folder does not exist, this method tries to create it; however, it does not resolve name conflicts. It means that if the destination folder contains a file with the same name as one of uploaded file, the file will be overwritten.

## Parsing POST Request Manually

Note

If you use IIS 7.5 you should disable request validation on the upload processing page (this page is specified with the UploadSettings.ActionUrl property).

<%@ Page Language="C#" ValidateRequest="false" %>

Another way lies in parsing the upload request manually via the Request object. However, firstly you should add a reference to the Aurigma.ImageUploaderFlash.dll from your project and add UploaderModule module to your web.config:

### XML

    <configuration>
       <system.web>
            <httpModules>
                <add name="UploaderModule" type="Aurigma.Uploader.UploaderModule"/>
            </httpModules>
       </system.web>
       <system.webServer>
            <modules>
                <add name="UploaderModule" type="Aurigma.Uploader.UploaderModule"/>
            </modules>
       </system.webServer>
    </configuration>

This module converts the upload request to a standard RFC1867 format.

The following code sample demonstrates how to save converted files and original file-related information using this approach.

### C#

    private void Page_Load(System.Object sender, System.EventArgs e)
    {
        string galleryPath = "Gallery/";
        int fileCount = int.Parse(Request[PostFields.PackageCount]);

        for (int i = 0; i < fileCount; i++)
        {
            string originalFileName = Request[string.Format(PostFields.SourceName, i)];

            // save original file
            string physGalleryPath = Server.MapPath(galleryPath);
            string sourceFileName = GetSafeFileName(physGalleryPath, originalFileName);
            if (Request.Files[string.Format(PostFields.File, 0, i)] != null)
            {
                Request.Files[string.Format(PostFields.File, 0, i)].SaveAs(System.IO.Path.Combine(physGalleryPath, sourceFileName));
            }

            // save thumbnail
            string physThumbnailsPath = Server.MapPath(galleryPath + "Thumbnails/");
            string thumbnailFileName = GetSafeFileName(physThumbnailsPath, originalFileName);
            if (Request.Files[string.Format(PostFields.File, 1, i)] != null)
            {
                Request.Files[string.Format(PostFields.File, 1, i)].SaveAs(System.IO.Path.Combine(physThumbnailsPath, thumbnailFileName));
            }

            //Save file info.
            XmlDocument descriptions = new XmlDocument();
            descriptions.Load(Server.MapPath(galleryPath + "Descriptions.xml"));

            XmlElement file = descriptions.CreateElement("file");
            file.SetAttribute("name", sourceFileName);
            file.SetAttribute("thumbName", thumbnailFileName);
            file.SetAttribute("width", Request[string.Format(PostFields.SourceWidth, i)]);
            file.SetAttribute("height", Request[string.Format(PostFields.SourceHeight, i)]);
            file.SetAttribute("description", Request[string.Format(PostFields.Description, i)]);

            descriptions.DocumentElement.AppendChild(file);

            //Save xml file with uploaded files info.
            descriptions.Save(Server.MapPath(galleryPath + "Descriptions.xml"));
        }
    }

    private string GetSafeFileName(string path, string fileName)
    {
        // ...
    }

Note

The upload script should return non-empty response.

To simplify accessing the POST request fields the ASP.NET control provides the PostFields class. This class exposes a number of fields which represent keys to Files and Form collections.

## See Also

### Reference

- ImageUploaderFlash Class
- FileUploadEventArgs Class
- AllFilesUploadEventArgs Class
- FileInfo Class
- UploadRequest Class
- UploaderModule Class
- PostFields Class

### Manual

- Quick Start with ASP.NET Control
- Configuring Upload Settings