Upload multiple files using AjaxFileUpload

Microsoft ASP.NET AjaxControlToolkit has recently published a multiple file upload. That allows you to upload multiple files at a time with Drag and drop user interface.

First we will see, how to use AjaxFileUpload control in asp.net site.

Open new project in Visual studio. I am using visual studio 2010.

In default page, add script manager, Update panel. You can also add update progress (optional).

For using AjaxFileUpload, you need to download latest AjaxControlToolkit from codeplex site. If you don't know how to add that to your visual studio then refer this link.

Now you can see AjaxFileUpload in Ajax Toolkit. Add that to update panel and edit the code as below.

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="jpg,jpeg,png,gif" MaximumNumberOfFiles="10" OnUploadComplete="File_Upload"/>

AllowedFileTypes : Defines which files are allowed to be uploaded

MaximumNumberOfFiles : Defines maximum number of files.

OnUploadComplete :  Defines the function for uploading the file. It runs when each file completes its uploading. Suppose, 5 files we selected to upload, then File_Upload functions run after 1st file uploaded, again runs when 2nd file uploaded and so on. But everything done is asynchronously and user doesnt find any break during uploading.


Now we will see the code behind it, in aspx.cs file

protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
   var ajaxUpload = (AjaxFileUpload)sender; 
   string strLongFilePath = e.FileName;
   string strFileExtension = System.IO.Path.GetExtension(strLongFilePath);
   char[] ch = new char[] { '\\' };
   string[] strPath = strLongFilePath.Split(ch);
   string strInternalFileName = DateTime.Now.ToFileTime().ToString() + strFileExtension;
   string strDestPath = Server.MapPath("~/Uploads/");
   ajaxUpload.SaveAs(@strDestPath + strInternalFileName);
  //Save path, filename to database here

}

Your files can be uploaded now.

Build and Run the project.







Comments

  1. I can not access other controls from the Uploaded method. Like-
    ajaxUpload.SaveAs(@strDestPath + strInternalFileName + TextBox1.Text);

    Here TextBox1.Text returns null

    ReplyDelete
    Replies
    1. Yes, that is limit of this uploader.
      one thing you can do is, store the image file name in session in advance.

      1 possible sequence is like below.
      1st page: enter image name
      2nd page: upload images

      For uploading multiple images, then you can take one common name and then increment counter.

      for example
      1st page: enter image name (asp)

      2nd page: upload images

      save it like
      asp1
      asp2
      asp3..and so on.

      So the conclusion is that, you can not take other control's value in Ajaxupload control. You have to store in session variable or generate random file name and caption and save it.

      [this is limitation of this control, as of my knowledge, till today they haven't worked on it]

      Delete
  2. how can i find the count of Selected files to upload

    ReplyDelete
    Replies
    1. Put one counter variable in below mentioned function. Each time it will increase and you will come to know.

      protected void File_Upload(object sender, AjaxFileUploadEventArgs e)

      Let me know if you still didn't get it.

      Delete
  3. How much long(200mb) file we can upload by this bcoz when i am upload a file greater than 200mb than i am facing error

    ReplyDelete
    Replies
    1. If you want to upload big files, you can set the in web.config under system.web. In the above example you can upload file size upto 1GB.

      Delete
  4. Helloo!
    Great, but the sample is no more available =(

    ReplyDelete
  5. Hi! JijaxXx..!! Link has been updated. Now you can download. :)

    ReplyDelete
  6. Hi, is there any way to send files with non-English letters in source path (e.g. polish letters: ł, ą ę etc.)?
    Even on site:
    http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx
    if you upload files with this letters, there are no previews for them an their size is grather than source ( they are corrupted and cant be opened on server side)
    Maybe there is some way to denny user to upload such a files?

    ReplyDelete
    Replies
    1. Clear your question. So do you want to allow user to upload file with non-english letters or want to restrict.?

      Delete
    2. i want to receive unharmed files on server side. The best way for me to achieve this is to "repair" AjaxFileUpload. I spent a lot of time searching for answer for this problem, but didn't find anything, so, if there is no way to "repair" it(???), i would like at least to forbid users to send files which have non-English letters in full path or file name. The worst solution which i would like to find (if previous are not reachable) is to automatically detect sent files corruption on server side, but the problem is that i cant find files full path or size on client side before they are send on server (when i sent files with non-english letters on server side they have larger size than on client side)

      Delete
    3. Sorry for late reply: Rename the file names to english letter while saving in server. Store that file name in database and also have the column in database with original non-english letters in same entry. Hope this might helps.

      Delete

Post a Comment

Popular posts from this blog

ICT dialler installation

Install Pear in Wamp Server 2.2 (with php 5.3.8) for windows