Click here to Skip to main content
15,886,026 members
Articles / Web Development / HTML

ASP.NET Server Side Handler for HTML5 Valums Ajax file Upload

Rate me:
Please Sign up or sign in to vote.
4.55/5 (15 votes)
27 May 2011CPOL1 min read 91.6K   5.5K   31   25
Implementation of ASP.NET handler for Valums ajax upload; A HTML 5 file uploader supports multiple file upload with progress bar, drag-and-drop

Introduction

This Ajax uploader uses XHR for uploading multiple files with progress-bar in FF3.6+, Safari4+, Chrome and falls back to hidden iframe based upload in other browsers, providing good user experience everywhere. You can also see PHP demo in Valums site at http://valums.com/ajax-upload/.

AjaxUpload.JPG

Background

The current implementation has server side handler for Java, PHP and Perl. But ASP.NET handler does not exist. Here, I have implemented an ASP.NET handler for Ajax file upload that supports Internet Explorer, Firefox and Chrome. 

Using the Code

The problem is that Internet Explorer uses context.Request.Files[] for sending file to server. But Firefox and Chrome use Context.Request.InputStream. So in handler, you need to check both for reading stream.

For Firefox and Chrome, you get fileName from header like:

C#
String filename = HttpContext.Current.Request.Headers["X-File-Name"];

Code that works in Firefox and Chrome is as follows:

C#
//This works for Firefox and Chrome.
Stream inputStream = 
HttpContext.Current.Request.InputStream;<br />FileStream fileStream = new 
FileStream(mapPath + "\\" + filename, 
FileMode.OpenOrCreate);inputStream.CopyTo(fileStream);fileStream.Close();

context.Response.Write("{success:true, name:\"" + filename + "\", path:\"" + 
path + "/" + filename + "\"}");

But for Internet Explorer, you need to use:

C#
HttpPostedFile uploadedfile = context.Request.Files[0];

Code that works for Internet Explorer browser is:

C#
HttpPostedFile uploadedfile = context.Request.Files[0];
 filename = uploadedfile.FileName;
uploadedfile.SaveAs(mapPath + "\\" + filename);
 context.Response.Write("{success:true, name:\"" + filename + "\", path:\"" + 
path + "/" + filename + "\"}");

Here the response is sent as JSON string and you will get JSON object as response. You need to send {success:true} to make Ajax upload understand that file upload is successful, otherwise you can send false

History

  • 27th May, 2011: Initial post

License

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


Written By
Software Developer
Bangladesh Bangladesh
Name SYED MD. ABUL BASHAR
Email ID: miltoncse00@gmail.com

I am now working as software engineer in Malaysia. I am from Bangladesh and I have completed my B.Sc (Engg.) in CSE from Rajshahi University of Engineering and Technology (RUET).I spend much time in learning latest technology.

My LinkedIn Profile : http://bd.linkedin.com/in/miltoncse00[^]

My blog :http://ciintelligence.blogspot.com/[^]

Comments and Discussions

 
Questionphoto.jpg 200 Kb failed Pin
NDOGN6-Aug-13 0:13
NDOGN6-Aug-13 0:13 
I cannot upload files. What ASP.net parameter should I check? As soon as I press upload a message appear:
photo.jpg 200 kB Failed. How can I solve?
QuestionIE Crashes Pin
Farnaz_Farnaz18-Apr-13 5:33
Farnaz_Farnaz18-Apr-13 5:33 
QuestionIs it possible to select mutiple files at the same time Pin
Travelthrprog9-Apr-13 4:32
Travelthrprog9-Apr-13 4:32 
Questionit runs differently in IE9 Pin
Jade Lin2-Apr-13 20:52
Jade Lin2-Apr-13 20:52 
Questionupload successfully but.... Pin
Alenty18-Oct-12 2:22
Alenty18-Oct-12 2:22 
GeneralMy vote of 5 Pin
NoName_ark10-May-12 8:04
NoName_ark10-May-12 8:04 
BugFix for IE - invalid path Pin
Member 86106163-May-12 3:47
Member 86106163-May-12 3:47 
Questioni con't run the project Pin
senthilkumarpsk8-Mar-12 2:25
senthilkumarpsk8-Mar-12 2:25 
AnswerRe: i can't run the project Pin
pimpers14-Sep-12 2:47
pimpers14-Sep-12 2:47 
GeneralMy vote of 5 Pin
Member 332085211-Feb-12 5:43
Member 332085211-Feb-12 5:43 
QuestionI can't get the filename Pin
LamJason14-Dec-11 3:57
LamJason14-Dec-11 3:57 
AnswerRe: I can't get the filename Pin
Syed BASHAR14-Dec-11 6:28
Syed BASHAR14-Dec-11 6:28 
GeneralMy vote of 3 Pin
Jepy20-Oct-11 23:19
Jepy20-Oct-11 23:19 
QuestionFileName Pin
gooroo.Net19-Oct-11 5:06
gooroo.Net19-Oct-11 5:06 
QuestionSaving to database Pin
Dave Franco10-Sep-11 5:24
Dave Franco10-Sep-11 5:24 
AnswerRe: Saving to database Pin
Syed BASHAR10-Sep-11 6:02
Syed BASHAR10-Sep-11 6:02 
GeneralMy vote of 5 Pin
Saraf Talukder22-Aug-11 3:20
Saraf Talukder22-Aug-11 3:20 
QuestionWorked like a charm... Pin
rmarni16-Aug-11 19:34
rmarni16-Aug-11 19:34 
Questionfile size & remove button Pin
yefriend27-Jul-11 18:05
yefriend27-Jul-11 18:05 
AnswerRe: file size & remove button Pin
Syed BASHAR29-Jul-11 10:02
Syed BASHAR29-Jul-11 10:02 
QuestionThanks! Pin
BigJoe71416-Jul-11 10:01
BigJoe71416-Jul-11 10:01 
AnswerRe: Thanks! Pin
Member 332085211-Feb-12 5:42
Member 332085211-Feb-12 5:42 
Questionfile size Pin
severous260002-Jul-11 5:09
severous260002-Jul-11 5:09 
AnswerRe: file size [modified] Pin
Syed BASHAR7-Aug-11 21:46
Syed BASHAR7-Aug-11 21:46 
GeneralRe: file size [modified] Pin
jalalbabaei26-May-12 2:04
jalalbabaei26-May-12 2:04 

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.