Go to DotNetFunda.com
 Online : 851 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > Ajax style File Upload

Submit Article | Articles Home | Search Articles |

Ajax style File Upload

 Download source file
 Posted on: 8/3/2009 12:54:25 AM by Anup1252000 | Views: 7977 | Category: ASP.NET | Level: Advance | Print Article
In this article we are going to see the fileupload without postback.. We are going to do this by using ICallbackEventHandler..

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Introduction
In this article we are going to use FileUpload in ajax style by using IcallBackEventHandler.. By using this method performance of your application is going to increase .. You are going to see this article in c# while downloading you get in vb also..



Lets go through this application step by step

Step 1: 

Create a new website >select the programming language as c#> Name the project as FileuploadAjaxStyle

Step 2: 

Just drag and drop the HTML Fileupload and a button..  Double click on that double it will generate the script automatically.. Add the span and give it a id as message.. We are going to use this span to store the message..Page looks like this 



Step 3: 

Now lets go to the code behind.. Add ICallBackEventHandler interface to the page.. Now the page looks like this..


Step 4: 

Now Just register this page by adding GetCallBackEventReference..

In GetCallbackEventreference we are passing the argumet as arguments, we are calling the callback as results.. results is the place where we are going to store our result.. If error occurs then we are calling the function called onerror.. true means it results asynchronously without postback..

Step 5:

Now we are going to use WebClient in RaiseCallbackEvent which exist in the namespace System.Net..

     

Now u create anotherpage and give the name as FileuploadTarget.aspx and just run the FileuploadTarget.aspx.. copy the address from the address bar..

UploadFile Method takes 3 parameters as Address,Web method and the Filename.. When u upload the file it is stored in eventArgument..

Step 6:

Now lets add javascript to the Default.aspx page..


Step 7:

Now lets go to code behind of Default.aspx.. We need to return cbref in GetCallbackResult method..

 

Step 8:

Now lets go to the FileuploadTarget.aspx code behind..

We are using HttpPostedFile and retrieving the file from Default.aspx.. Next steps are all basic where i am pointing it to the folder called anup.. Checking whether that Directory exist, if not lets create one and saving the file to the directory which i have create..

Step 9: 

Now its a time to run the application



I hope you people like this application.

Thanks


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


About anup hosur

Experience:2 year(s)
Home page:
Member since:Saturday, August 01, 2009
Level:Starter
Status: [Member]
Biography:
 Latest post(s) from Anup1252000

   ◘ 4 steps to enable instrumentation in WCF posted on 3/14/2010 4:02:05 AM
   ◘ Ajax Enabled WCF Service posted on 2/15/2010 1:10:10 AM
   ◘ Membership provider with WCF posted on 2/11/2010 7:00:47 AM
   ◘ Creating Multiple Channels in Channel factory in WCF posted on 2/11/2010 4:06:00 AM
   ◘ WCF Hosting in Windows Services posted on 1/28/2010 7:53:50 AM


 Responses
Posted by: Sandy954 | Posted on: 03 Aug 2009 01:04:40 AM

hi anup,
this is sandy,
u done a gr8 job, this article really help mee alot.
thank u so much.
keep posting articles.

Regards.
sandy.

Posted by: Anup1252000 | Posted on: 03 Aug 2009 01:30:04 AM

Thanks for your comments sandy..

Posted by: SheoNarayan | Posted on: 04 Aug 2009 08:48:19 PM

Thanks for nice article Anup, Keep it up!

Posted by: Utsav | Posted on: 01 Sep 2009 02:09:05 AM

hello anup, thanks for your great article.
it helped me a lot.

i have used ur article on my project. can u help me on more validaing the file that has been uploaded by user. such as
file must not exceed more than 2mb
validating file with its extension.
checking whether the file has been selected or not.
etc.

with best regards,
Utsav

Posted by: Anup1252000 | Posted on: 01 Sep 2009 10:06:36 PM

hi utsav.. u need to write these lines of code in web.config under system.web.. <httpRuntime maxRequestLength="2097151" enable="true"/>

Posted by: Chuongxl | Posted on: 01 Oct 2009 10:58:59 PM

after downloaded source code

build :not error
but i can't upload file

i allway recieve messegage "upload failed"
please help me??

Posted by: Chuongxl | Posted on: 01 Oct 2009 10:58:59 PM

after downloaded source code

build :not error
but i can't upload file

i allway recieve messegage "upload failed"
please help me??

Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)