5,136,421 members and growing! (13,439 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Advanced License: The Code Project Open License (CPOL)

AJAX file upload

By Dzianis@gmail.com

This article described how to create file Upload form using AJAX technology.
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 27 Feb 2006
Updated: 27 Feb 2006
Views: 197,639
Announcements



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 3.54 Rating: 2.54 out of 5
6 votes, 24.0%
1
8 votes, 32.0%
2
1 vote, 4.0%
3
3 votes, 12.0%
4
7 votes, 28.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

File uploading thru HTTP is always big problem for websites. There are some restrictions from client and server sides. But with growing internet channels bandwidth one of major problem is file size. Sometimes it’s impossible to send 500MB file to webserver due to request length limit. One of the workaround is to increase maximal request length on webserver, but it may cause server restart when memory limit will be exceeded. For example: IIS APS.NET webserver. We increases maxRequestLength to 500MB, memoryLimit default value is 60% it means that process will be recycled when it use more than 60% of physical memory. If we have 1GB of physical memory in system and couple of users uploads simultaneously 400MB files webserver will be restarted with very high chance, because server wouldn’t have time to release memory from Requests objects.

Another big issue is file upload continuing, when process was interrupted by some reasons. Normally user needs to upload whole file once again

In this example I’ll describe how to implement file uploading method using AJAX and WebService technologies. Of course this method has its own restrictions, but it would be quite useful for intranet solutions and administrative areas in internet websites.

How it works

Main idea is quite simple. We should read file partially on send these parts to webserver.

Client Side

//Receive intial file information and init upload
function getFileParams()
{ 
 //Convert file path to appropriate format
 this.filePath = document.getElementById("file").value.replace(/\\/g, "\\\\");
 
 fso = new ActiveXObject( 'Scripting.FileSystemObject' );
 if ( !fso.FileExists(this.filePath) )
 {
  alert("Can't open file.");
  return;
 }
  
 f = fso.GetFile( this.filePath );
 this.fileSize = f.size;
 this.fileName = f.Name;
 InitStatusForm();
 InitUpload();
}

Allocate file on client, get file size. I use Scripting.FileSystemObject ActiveX object to get file size because this object will not load full file in memory.
Then init form Layout and upload process by InitStatusForm() and InitUpload functions.

function InitUpload()
{
 document.getElementById("uploadConsole").style.display = "none";
 document.getElementById("statusConsole").style.display = "block";
 
 xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
 xmlhttp.onreadystatechange = HandleStateChange;

 var parameters = "fileSize=" + encodeURI(this.fileSize) +
 "&fileName=" + encodeURI(this.fileName)+
 "&overwriteFile=" + encodeURI(document.getElementById("overwriteFile").checked);

 xmlhttp.open("POST","%22http://localhost/AJAXUpload/Upload.asmx/InitUpload%22">http://localhost/AJAXUpload/Upload.asmx/InitUpload", true);
 xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 xmlhttp.setRequestHeader("Content-length", parameters.length);
 xmlhttp.setRequestHeader("Connection", "close");
 xmlhttp.send(parameters);
}

Init upload: Create XmlHttp object and send to webservice initial information such file size, file name and overwrite flag.

//XMLHTTPRequest change state callback function
function HandleStateChange() {
 switch (xmlhttp.readyState) {
 case 4:
  response  = xmlhttp.responseXML.documentElement;
  id = response.getElementsByTagName('ID')[0].firstChild.data;
  offset = esponse.getElementsByTagName('OffSet')[0].firstChild.data;
  bufferLength = response.getElementsByTagName('BufferLength')[0].firstChild.data;
  
  percentage = (offset/this.fileSize)*100;
  if (offset<this.fileSize && !this.cancelUpload)
  {
   UpdateStatusConsole(percentage, "Uploading");
   SendFilePart(offset, bufferLength);
  }
  else
  {
   SetButtonCloseState(false);
   if (this.cancelUpload)
    UpdateStatusConsole(percentage, "Canceled");
   else
    UpdateStatusConsole(percentage, "Complete");
  }
  break;
 } 
}

Asynchronous request from server-side handled by HandledStateChange() callback function.
Parse parameters from server: id – response-request identifier, offset – start position to read file part. bufferLength – file block size to read.
If requested start position not exceed file size and upload not canceled by user we send file part.

//Read part of file and send it to webservice
function SendFilePart(offset, length)
{
 // create SOAP XML document
 var xmlSOAP = new ActiveXObject("MSXML2.DOMDocument");
 xmlSOAP.loadXML('<?xml version="1.0" encoding="utf-8"?>'+
 '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '+
 'xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> '+
  '<soap:Body>'+
   '<UploadData xmlns="http://tempuri.org/" >'+
    '<fileName>'+this.fileName+'</fileName>'+
    '<fileSize>'+this.fileSize+'</fileSize>'+
    '<file></file>'+
   '</UploadData>'+
  '</soap:Body>'+
 '</soap:Envelope>');
 
 // create a new node and set binary content
 var fileNode = xmlSOAP.selectSingleNode("//file");
 fileNode.dataType = "bin.base64";
 // open stream object and read source file
 if (adoStream.State != 1 )
 {
  adoStream.Type = 1;  // 1=adTypeBinary 
  adoStream.Open(); 
  adoStream.LoadFromFile(this.filePath);
 }
 
 adoStream.Position = offset;
 // store file content into XML node
 fileNode.nodeTypedValue = adoStream.Read(length);//adoStream.Read(-1); // -1=adReadAll
 if (adoStream.EOS)
 {
  //Close Stream
  adoStream.Close();
 }
 
 // send XML document to Web server
 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 xmlhttp.onreadystatechange = HandleStateChange;
 xmlhttp.open("POST", "%22http://localhost/AJAXUpload/Upload.asmx%22">http://localhost/AJAXUpload/Upload.asmx", true);
 xmlhttp.setRequestHeader("SOAPAction", "%22http://tempuri.org/UploadData%22">http://tempuri.org/UploadData");
 xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
 xmlhttp.send(xmlSOAP);
}

In this function we create XmlSoap, read file part using ADODB.Stream ActiveX object and send it to WebServer with file name and filesize.
Server response from this operation would be handled by the same HandledStateChange() callback function.


Server Side

[WebMethod]
public XmlDocument InitUpload(int fileSize, string fileName, bool overwriteFile )
{
 long offset = 0;
 string filePath = GetFilePath(fileName);
 
 if (File.Exists(filePath))
 {
  if (overwriteFile)
  {
   File.Delete(filePath);
  }
  else
  {
   using (FileStream fs = File.Open(filePath, FileMode.Append)) 
   {
    offset = fs.Length;
   }
  }
 }

 return GetXmlDocument(Guid.NewGuid(), string.Empty, offset, 
(InitialBufferLength+offset)>fileSize?(int)(fileSize-offset):InitialBufferLength);
}

Init upload server-side function. If file with such name already exist and overwrite flag is false existed file will be appended otherwise file will be deleted. Then construct response by GetXmlDocument function.

[WebMethod]
public XmlDocument UploadData(string fileName, int fileSize, byte[] file)
{
 if (fileName == null || fileName == string.Empty || file == null)
  return GetXmlDocument(Guid.NewGuid(), "Incorrect UploadData Request", 0, 0);

 string filePath = GetFilePath(fileName);

 long offset=0;
 using (FileStream fs = File.Open(filePath, FileMode.Append)) 
 {
  fs.Write(file, 0, file.Length);
  offset = fs.Length;
 }
 return GetXmlDocument(Guid.NewGuid(), string.Empty, offset, 
(InitialBufferLength+offset)>fileSize?(int)(fileSize-offset):InitialBufferLength);
}

This method handle request from client with file part data. Append file part to uploaded part and request next part.

Install and Run

To run project you should done couple manipulations:
1. Give read/write permissions to your IIS user for Upload folder.
2. Enable ActiveX objects in your IE browser. (Add website to trusted websites)

Remarks

I’ve described solution core all Layout functionality like upload panel and progress bar can be founded in project.

Please don’t use this solution AS IS in your projects. It’s just AJAX upload form example.
Working with streams, files and ActiveX objects expects handles all error cases.

Links

http://codeproject.com/Ajax/JavaScriptSOAPClient.asp - Excellent AJAX article.
http://www.15seconds.com/issue/010522.htm - File upload technique.

License

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

About the Author

Dzianis@gmail.com


Graduated in Belurassian State University on chair of Artificial Intelligence.
Take a part in development video alarm system for PC with live AI analyzing multiple video streams.

3 years of ASP.NET experience in development multi-thier applications, integrated applications, enterprise level solutions.

Almost 1 year experience in ASP.NET 2.0 starting from BETA 2.

Now working in
http://pro-softsolutions.com
Occupation: Web Developer
Location: Belarus Belarus

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralADODB.Stream loads whole file into memorymemberwzhao20007:14 21 Nov '07  
QuestionAJAX Fileupload Automation Errormemberjpthomas0:45 27 Oct '07  
GeneralADODB.StreammemberDizzyinc22:57 27 Sep '07  
GeneralRe: ADODB.Streammembermosejas11:57 24 Oct '07  
Questiona question from KrishnaadminSean Ewington8:56 11 Jun '07  
GeneralStill not working on SP2 & IE7memberIsmail Sidhpuri23:01 10 Jun '07  
GeneralHi compatriot, I wonder if it's possible to run it on windows with sp2 installed. [modified]memberandrew.golik1:54 21 Mar '07  
GeneralRe: Hi compatriot, I wonder if it's possible to run it on windows with sp2 installed.membermagicxman0:47 6 Sep '07  
GeneralWorks Great After Tweakingmemberkodhedz3:03 15 Mar '07  
GeneralRe: Works Great After Tweakingmemberandrew.golik2:09 21 Mar '07  
QuestionadoStream.state is null or not an objectmemberCan Gunaydin6:54 22 Jan '07  
GeneralFailed to create fso, new ActiveXObject("Scripting.FileSystemObject"); memberanhboston120:03 3 Dec '06  
GeneralRe: Failed to create fso, new ActiveXObject("Scripting.FileSystemObject"); membertejas_chonkar7:14 24 Jan '07  
Questionit didn/t work with mememberSyri21:31 14 Nov '06  
AnswerRe: it didn/t work with mememberDzianis@gmail.com23:27 15 Nov '06  
GeneralRe: it didn/t work with mememberJohnny McSpankwit2:14 13 Dec '06  
AnswerRe: it didn/t work with memembermagicxman21:04 21 Aug '07  
GeneralGmail attachmentmembersmil3:01 14 Jun '06  
GeneralNot Woking Over Subnetmembergrbala10:16 2 May '06  
GeneralRe: Not Woking Over Subnetmemberahmadzaro21:46 2 May '06  
GeneralRe: Not Woking Over SubnetmemberDzianis@gmail.com2:25 8 May '06  
GeneralRe: Not Woking Over Subnet [modified]memberahmadzaro2:39 19 Jun '06  
GeneralAutomation server can't create objectmemberYoussef Saad12:04 29 Apr '06  
GeneralRe: Automation server can't create objectmemberfribbs19:32 13 May '06  
QuestionMicrosoft JScript runtime error: 'response' is null or not an objectmemberbaimos21:37 30 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Feb 2006
Editor: Sean Ewington
Copyright 2006 by Dzianis@gmail.com
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project