Upload multiple files using AjaxFileUpload
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.
I can not access other controls from the Uploaded method. Like-
ReplyDeleteajaxUpload.SaveAs(@strDestPath + strInternalFileName + TextBox1.Text);
Here TextBox1.Text returns null
Yes, that is limit of this uploader.
Deleteone 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]
how can i find the count of Selected files to upload
ReplyDeletePut one counter variable in below mentioned function. Each time it will increase and you will come to know.
Deleteprotected void File_Upload(object sender, AjaxFileUploadEventArgs e)
Let me know if you still didn't get it.
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
ReplyDeleteIf 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.
DeleteHelloo!
ReplyDeleteGreat, but the sample is no more available =(
Hi! JijaxXx..!! Link has been updated. Now you can download. :)
ReplyDeleteHi, is there any way to send files with non-English letters in source path (e.g. polish letters: ł, ą ę etc.)?
ReplyDeleteEven 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?
Clear your question. So do you want to allow user to upload file with non-english letters or want to restrict.?
Deletei 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)
DeleteSorry 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