The ASP FileUpload control can be handy if you are working with Web Forms as a means to easily integrate file uploading into your application and aren't experienced enough to work directly with HTML file inputs, Flash or jQuery based alternatives.
The Problem
Meet our friend the FileUpload Control:
- <!-- File Uploader -->
- <asp:FileUpload ID="files" name="files[]" runat="server" multiple="multiple" />

Which can be created using the following code:
- <!-- The necessary scripts that do work son. -->
- < script type = 'text/javascript' > function handleFileSelect(evt)
- {
- // FileList object
- var files = evt.target.files;
- // Loop through the FileList and render image files as thumbnails.
- for (var i = 0, f; f = files[i]; i++)
- {
- // Only process image files.
- if (!f.type.match('image.*'))
- {
- continue;
- }
- var reader = new FileReader();
- // Closure to capture the file information.
- reader.onload = (function (theFile)
- {
- return function (e)
- {
- // Render thumbnail.
- var span = document.createElement('span');
- // Creates a thumbnail with an onclick function
- // that will handle the mock deletion
- span.innerHTML = ['<img class="thumb" src="', e.target.result, '" title="', escape(theFile.name), '" onclick="deleteImage(this);"/>'].join('2015-02-03 05:12:55');
- document.getElementById('list').insertBefore(span, null);
- };
- })(f);
- // Read in the image file as a data URL.
- reader.readAsDataURL(f);
- }
- } < /script>
- // The image was clicked delete it
- function deleteImage(imgToDelete) {
- imgToDelete.style.display = "none";
- }
- // Hasta La Vista Files!
- document.getElementById('yourFileUpload').value = "";
So how could you handle removing images that are already present within your Request.files collection, just waiting to be uploaded? Well, here is a simple workaround to handle that.
Place a hidden field within your <form> element that houses your FileUpload control to store all of the files that are marked for deletion:
- <asp:HiddenField ID="filesToIgnore" runat="server" />
- function delClick(imgToDelete)
- {
- // Add this line (your thumbnail will need to reference
- // a file name or property from your actual file – this
- // example stores the FileName in the title property
- document.getElementById('filesToIgnore').value += (imgToDelete.title + ",");
- // Additional code here to remove the thumbnail
- imgToDelete.style.display = “none”;
- }
When your form is posted, you will need to create a collection based on your hidden filesToIgnorefield within your Page Load or related event:
- // Upload click event
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- // Get files to be ignored (not uploaded)
- string[] ignoredFiles = filesToIgnore.Value.Split(new char[]
- {
- ','
- }, StringSplitOptions.RemoveEmptyEntries);
- // Iterate through posted files
- HttpFileCollection uploadFiles = Request.Files;
- for (int i = 0; i < uploadFiles.Count; i++)
- {
- HttpPostedFile postedFile = uploadFiles[i];
- // If it isn't a file meant for deletion - don't upload
- if (!ignoredFiles.Any(c => postedFile.FileName.Contains(c)))
- {
- // Upload your file here
- Response.Write(String.Format("{0} Uploaded!<br />", postedFile.FileName));
- }
- }
- }
Although this example was a very hasty one, there are many methods of handling this. You may want to seriously consider using one of the many jQuery based plug-ins that supports multiple file uploads or creating your own solution that uses a single file upload element for each file, which would allow you to manage them independently.
Flash plugins can often be great at handling File Upload operations as well; although many people including myself cringe that the sight of that five-letter F-word.


Ekrem TapanPosted Oct 28, 2015, 11:33 PM EDT
good article
Mohammed IbrahimPosted Oct 28, 2015, 5:23 AM EDT
nice
Sibeesh VenuTOP 100Posted Oct 28, 2015, 3:27 AM EDT
Nice Share
Pramod KumarPosted Oct 27, 2015, 11:32 PM EDT
Great One
Francis SusaimichaelPosted Oct 27, 2015, 6:07 PM EDT
Welcome Rion!!
Nilesh JadavPosted Oct 27, 2015, 5:51 PM EDT
Nice share sir !