Click here to Skip to main content
15,861,125 members
Articles / Web Development / ASP.NET
Article

Multiple File Upload With Progress Bar Using Flash and ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (297 votes)
10 Jul 2008CPOL6 min read 5.3M   42.9K   953   1.1K
How to use Flash to upload multiple files in a medium-trust hosting environment
Image 1

Image 2

Introduction

It is difficult to find a decent upload control that handles large files, shows a progress bar, and still works in a medium-trust hosting environment. The problem with these controls is that medium-trust does not allow Reflection. But this does not mean that we are out of luck. One of the problems these controls strive to overcome is the server running out of memory when large files are uploaded by streaming the uploaded file to disk. But the HttpPostedFile class description states that "By default, all requests, including form fields and uploaded files, larger than 256 KB are buffered to disk, rather than held in server memory." So, we don't have to worry about this problem. The other issue is feedback to the user by way of progress bar. Enter Flash's fileReference API, which allows you to upload a file from Flash, and also keep track of it. FileReferenceList allows the user to select multiple files.

Using the Code

Please read the article and the comments in the source code before asking questions in the forums.

I have finally gotten around to updating the Flash component of this article. I have re-written it using Flex, and have tried to add some of the suggestions from the message board. Some of the new features include adding files without clearing previously added files, removing individual files, individual progress bar as well as total progress bar, cancelling and then uploading does not re-upload previously uploaded files, ability to add and remove files while uploading files. The code is written using code behind and should be easier to modify. Moreover, all you need to make changes is a text editor and then you can compile it using the Flex 2 SDK, which is a free download from Adobe. Error handling should be better as well, though additional code may need to be added to handle it better. I have left the old fileUpload.swf and fileUpload.fla file in the source code in case anyone still wanted it. The source for the new FlashFileUpload.swf is contained in the FlashFileUpload_src.zip file and contains five files:

  • FlashFileUpload.mxml - Main application GUI
  • components\ApplicationClass.as - Main application code behind
  • components\FileUpload.as - FileUpload class used for individual file uploading
  • components\FileUploadEvent.as - A custom event class
  • components\FileUploadProgressChangedEvent.as - A custom event class

I won't put the file's code in the article, you can download it and read the comments in the code. Also, a small declaration: I have little ActionScript experience, only from the first upload control and this one. Neither do I have any Flex experience. This is the first time I have looked at Flex. So if I did something in the code that is not best practice, or if it could have been done better another way, it is due to my inexperience with Flex and ActionScript and the small amount of time I put into writing it. I give no guarantees for this control. I hope you find this article helpful.

The code isn't very complicated, and is pretty well commented. I have now encapsulated the Flash object generation into a server control and embedded the FlashFileUpload.swf file in the DLL. See the comments in Default.aspx in the UploadPage folder for options and usage. I've included options to limit the file types to upload as well as limiting individual file size and total file size and can all be set using the server control. Also, setting the upload page, query parameters, and the JavaScript function called when completed are easily set using the server control.

I have included a number of examples for doing things with the upload control. This includes using a HttpHandler as the upload page (lighter than using an ASPX page), using forms based authentication, and using sessions. I'll go over each real quick here, but will refer you to the source code for the actual code.

The uploading of the files are done using Flash (FlashFileUpload.swf) and can be used in any server side language, such as PHP, Java, Coldfusion, ASP, etc. If it can handle file uploads, it can use the Flash control. What the source code goes over is how to use it in ASP.NET.

The first step in using it in ASP.NET is to add the server control to a Web form (see UploadPage/Default.aspx) and setting the upload page in the server control's properties. The upload page can be any page that can handle uploaded pages, even another ASPX page. The preferred method is to use a HttpHandler because it is lighter than an ASPX page. For an HttpHandler, all that is required is a class that implements IHttpHandler and some settings in the <httpHandlers> section of the web.config. Also setting the maxRequestLength in the <httpRuntime /> section would also be a good idea since this limits the size of file that can be uploaded to the server. The default is something like 2 or 4 megabytes. So you want to set this to the maximum allowed file size you want uploaded. That is all that is needed to use the upload control. The rest of the code is for the examples stated above.

Cookies and Flash

There is a bug in Flash and non-Internet Explorer browsers. From what I have read, Flash does not send cookies that were set in the browser with the uploaded file. This means that anything that uses cookies will not be accessible in the upload page. This means that sessions and forms authentication break using their default implementation, because they use cookies. This usually results in the dreaded #2038 error. There are some workarounds.

Forms Authentication

To get around the Flash bug problem using forms authentication, create a folder and add a Web form to the folder. This will be the upload page so add the upload control. Add a web.config file marking the folder accessible to anonymous users. Then in the page load of the Web form, check if the user is authenticated, sending them to the login page if they aren't. Then use the following code to encode the users identity ticket and put it in the query parameters of the upload control:

C#
FormsIdentity cIdentity = User.Identity as FormsIdentity;
string encryptString = FormsAuthentication.Encrypt(cIdentity.Ticket);
flashUpload.QueryParameters = string.Format("User={0}", encryptString);

Then in the upload page, the identity can be extracted:

C#
string EncryptString = context.Request.QueryString["User"];
FormsAuthenticationTicket UserTicket = FormsAuthentication.Decrypt(EncryptString);

And we can check if it is expired, or even get the user's username:

JavaScript
if(!UserTicket.Expired)
{
    string username = UserTicket.Name;
}

Sessions

Unfortunately sessions aren't quite as clean. In order to use them, sessions must be set to be cookieless. Doing this adds some gibberish to the URL. Usually it is transparent to the user. Set it in the web.config:

XML
<sessionState cookieless="UseUri" />

If using an HttpHandler, it needs to implement IRequiresSessionState as well.

Quick Note

The source code solution was built using Visual Studio 2008, but is .NET 2.0 based. If you do not have Visual Studio 2008 (not sure if the free Web developer studio can open the solution or not), you can still open the Web project under FlashUpload_Web in Visual Studio 2005 or the free Web developers by going to 'File' and selecting 'Open Web Site' and browsing to the FlashUpload_Web folder.

History

  • 09.28.2006 -- Initial post
  • 07.10.2008 -- Source updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Size of the flash window Pin
salesky24-Aug-08 21:29
salesky24-Aug-08 21:29 
GeneralRe: Size of the flash window Pin
darick_c25-Aug-08 4:31
darick_c25-Aug-08 4:31 
Questiontinymce how to integrate it to my project Pin
nibinki3335-Aug-08 21:50
nibinki3335-Aug-08 21:50 
Questionpdf files in asp.net Pin
nibinki3335-Aug-08 21:43
nibinki3335-Aug-08 21:43 
GeneralAdding thumbnails Pin
John Redfield5-Aug-08 12:48
John Redfield5-Aug-08 12:48 
GeneralRe: Adding thumbnails Pin
darick_c5-Aug-08 14:52
darick_c5-Aug-08 14:52 
GeneralOne more way to get around Forms Authentication in Firefox Pin
coopey2474-Aug-08 9:40
coopey2474-Aug-08 9:40 
GeneralRe: One more way to get around Forms Authentication in Firefox Pin
jimkqui12-Aug-08 7:09
jimkqui12-Aug-08 7:09 
My upload page is in http://mydomainname.com/upload.aspx
my upload destination folder is in http://mydomainname.com/resource/uploadfolder/
I am using a masterpage.
Yes, I use membership FORM authentication. This means ONLY logged in (authenticated users) can upload.

Is your solution solving my problem?
As you can see from above, my "upload destination folder" is in /resource/uploadfolder/, not inside the root folder.

i am a bit confused with your explanation.
Do i put:

<location path="controls">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>

in my /resource/uploadfolder/ OR htt://mydomainname.com/

Please reply.
I am almost to the point of despair.
GeneralRe: One more way to get around Forms Authentication in Firefox Pin
darick_c21-Aug-08 5:41
darick_c21-Aug-08 5:41 
GeneralRe: One more way to get around Forms Authentication in Firefox Pin
coopey24727-Aug-08 10:24
coopey24727-Aug-08 10:24 
QuestionIntegrated Windows Authentication Login Box Pin
sunsmile4-Aug-08 4:45
sunsmile4-Aug-08 4:45 
AnswerRe: Integrated Windows Authentication Login Box Pin
darick_c4-Aug-08 4:59
darick_c4-Aug-08 4:59 
GeneralRe: Integrated Windows Authentication Login Box Pin
sunsmile4-Aug-08 6:29
sunsmile4-Aug-08 6:29 
GeneralRe: Integrated Windows Authentication Login Box Pin
felix4515-Sep-08 15:24
felix4515-Sep-08 15:24 
GeneralHi Pin
Khalvati mohammad2-Aug-08 20:48
Khalvati mohammad2-Aug-08 20:48 
GeneralHi Pin
Khalvati mohammad2-Aug-08 20:48
Khalvati mohammad2-Aug-08 20:48 
GeneralSuggestions for the next version. Pin
Sam Rahimi18-Jul-08 6:09
Sam Rahimi18-Jul-08 6:09 
GeneralRe: Suggestions for the next version. Pin
darick_c18-Jul-08 6:54
darick_c18-Jul-08 6:54 
GeneralFire Fox & Progress Bar Issues Pin
Member 468577617-Jul-08 19:00
Member 468577617-Jul-08 19:00 
GeneralHelp me! .How Error can i report when catch file size not suppot or when i upload successfully. [modified] Pin
Member 365049717-Jul-08 0:55
Member 365049717-Jul-08 0:55 
GeneralError #2038 and IIS 7 Pin
typhoid16-Jul-08 14:10
typhoid16-Jul-08 14:10 
GeneralRe: Error #2038 and IIS 7 Pin
CowKiller6-Jan-10 17:27
CowKiller6-Jan-10 17:27 
GeneralRe: Error #2038 and IIS 7 Pin
kjhaveri13-Aug-10 1:13
kjhaveri13-Aug-10 1:13 
GeneralRe: Error #2038 and IIS 7 Pin
typhoid13-Aug-10 2:18
typhoid13-Aug-10 2:18 
QuestionHTML Usage Pin
skindy16-Jul-08 9:53
skindy16-Jul-08 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.