using multiple file uploader to upload image in asp.net c#

Microsoft'southward .Net has come up a long way when information technology comes to uploading files, various types of files. With Asp.Net version two.0, information technology has introduced the FileUpload server side control for the very first time. This has near simplified the way files are uploaded using Asp.Net. I'll show you how easily you can upload multiple files in Asp.Cyberspace using C# and VB.

Multiple File Upload

Bank check browser support

👉 You may too like this postal service... How to upload Multiple files using HTML5, jQuery Ajax and Asp.Net Web API
Upload files using HTML5 and Web API

For file upload, we will use Asp.Net FileUpload control with HttpFileCollection class. In addition, we volition besides run into how we can restrict or limit the number files to exist uploaded. The limit nosotros will fix in our case is 10.

Limiting or restricting the number files for upload using Asp.Net has its advantages.

01) It will non put unnecessary burden on the Host server, and relieve valuable bandwidth.
02) We can efficiently upload multiple files at a time, and in the procedure bank check the permissible size, type of files etc, at the server side using code behind procedures.

Nosotros will as well check for duplicate files earlier uploading the files to the server, ignoring the files extension. That is, we will non upload more than than ane file with the same proper name, irrespective of the file type.

For example: If the first file is 1.jpeg and information technology is uploaded, then ane.pdf volition be considered duplicate and will not exist uploaded.

The Asp.Net FileUpload command is a combination of a Textbox and a Push button control. Together it will let united states of america to select file(s) to upload at a remote location. Clicking the button will pop upwardly a Dialog box or a Window, which will prove a list of Folders and Files in the drive of your local machine. The Textbox volition evidence the total path with the selected file(s) name.

Past default, the FileUpload control volition allow the users to select only a unmarried file at a time. This is how it looks when you lot first add it to the web page.

<asp:FileUpload ID="fileUpload" runat="server" />

To permit users to select Multiple files simply add together multiple="true".

<asp:FileUpload ID="fileUpload"            multiple="true"            runat="server" />

Finally, we will upload the files using Asp.Net HttpFileCollection class. This grade will continue an eye on all the files uploaded from customer side. In that location past giving vital data nigh the file size, type etc. Along with it nosotros will employ Organization.IO namespace, which will allow united states of america to cheque for duplicate files in a detail Folder or Directory on the server.

You can too apply jQuery Multifile Plug-in to upload multiple files quickly and efficiently in Asp.Internet. This plug-in has Select and Un-select options.

The Markup

<!DOCTYPE html> <html> <head>     <title>Multiple File Upload</title>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/one.7.1/jquery.min.js"></script> </head> <body>     <grade id="form1" runat="server">         <div id="divFile">             <h3>Multiple File Upload in Asp.Internet (C#)</h3>                          <p>            <asp:FileUpload            ID="fileUpload"            multiple="true" runat="server" />             </p>             <p>            <asp:Button            ID="btUpload" Text ="Upload Files"            OnClick="Upload_Files" runat="server" />             </p>                           <p><asp:label id="lblFileList" runat="server"></asp:label></p>             <p><asp:Label ID="lblUploadStatus" runat="server"></asp:Label></p>             <p><asp:Label ID="lblFailedStatus" runat="server"></asp:Characterization></p>         </div>     </form> </torso>

The Script

We'll as well practise a simple validation at the client side. Check if the user has selected any file before calling the server side part.

<script>     $('#btUpload').click(function() {          if (fileUpload.value.length == 0) {                 warning('No files selected.');                     return false;          }      }); </script> </html>

👉 Now, if you want to bank check the file size before uploading, you must read this mail service.
Get the file size in C#

Code Behind (C#)

using System; using System.IO; using System.Web;  public partial grade _Default : System.Spider web.UI.Page  {     protected void Upload_Files(object sender, EventArgs e)     {         if (fileUpload.HasFile)              {             int iUploadedCnt = 0;             int iFailedCnt = 0;            HttpFileCollection            hfc = Asking.Files;             lblFileList.Text = "Select <b>" + hfc.Count + "</b> file(southward)";              if (hfc.Count <= 10)                 {                 for (int i = 0; i <= hfc.Count - one; i++)                 {                     HttpPostedFile hpf = hfc[i];                     if (hpf.ContentLength > 0)                     {                         if (!File.Exists(Server.MapPath("CopyFiles\\") +                              Path.GetFileName(hpf.FileName)))                         {                             DirectoryInfo objDir =                                  new DirectoryInfo(Server.MapPath("CopyFiles\\"));                              string sFileName = Path.GetFileName(hpf.FileName);                             cord sFileExt = Path.GetExtension(hpf.FileName);                                                           FileInfo[] objFI =                                  objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");                              if (objFI.Length > 0)                             {                                                                  foreach (FileInfo file in objFI)                                 {                                     string sFileName1 = objFI[0].Name;                                     string sFileExt1 =            Path.GetExtension<(objFI[0].Name);                                      if (sFileName1.Replace(sFileExt1, "") ==                                              sFileName.Replace(sFileExt, ""))                                     {                                         iFailedCnt += i;                                                 intermission;                                     }                                 }                             }                             else                             {                                                                  hpf.SaveAs(Server.MapPath("CopyFiles\\") +                                      Path.GetFileName(hpf.FileName));                                 iUploadedCnt += ane;                             }                         }                     }                 }                 lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded.";                 lblFailedStatus.Text = "<b>" + iFailedCnt +                      "</b> duplicate file(southward) could non be uploaded.";             }             else lblUploadStatus.Text = "Max. 10 files immune.";         }         else lblUploadStatus.Text = "No files selected.";     } }

Vb.Net

Choice Explicit On Imports System.IO  Fractional Class _Default     Inherits Organization.Web.UI.Page      Protected Sub Upload_Files(sender Every bit Object, eastward Equally EventArgs)                   If fileUpload.HasFile And then             Dim iUploadedCnt As Integer = 0             Dim iFailedCnt Equally Integer = 0             Dim hfc As            HttpFileCollection            = Request.Files              lblFileList.Text = "Select <b>" & hfc.Count & "</b> file(s)"              If hfc.Count <= 10 And so                              For i As Integer = 0 To hfc.Count - ane                     Dim hpf Every bit            HttpPostedFile            = hfc(i)                     If hpf.ContentLength > 0 And then                         If Not File.Exists(Server.MapPath("CopyFiles\") & _                                  Path.GetFileName(hpf.FileName)) So                              Dim objDir As New            DirectoryInfo(Server.MapPath("CopyFiles\"))                              Dim objFI As FileInfo() = _                                 objDir.GetFiles(Replace(Path.GetFileName(hpf.FileName), _                                     Path.GetExtension(hpf.FileName), "") & ".*")                             If objFI.Length > 0 Then                                                                  For Each file As FileInfo In objFI                                     If Replace(objFI(0).Name, Path.GetExtension(objFI(0).Name), "") = _                                         Supervene upon(Path.GetFileName(hpf.FileName),                                              Path.GetExtension(hpf.FileName), "") Then                                          iFailedCnt = iFailedCnt + 1                                         Exit For                                     End If                                 Side by side                             Else                                                                  hpf.SaveAs(Server.MapPath("CopyFiles\") & _                                         Path.GetFileName(hpf.FileName))                                 iUploadedCnt = iUploadedCnt + 1                             End If                         Cease If                     Terminate If                 Next i                 lblUploadStatus.Text = "<b>" & iUploadedCnt & "</b> file(s) Uploaded."                 lblFailedStatus.Text = "<b>" & iFailedCnt & _                     "</b> indistinguishable file(s) could not exist uploaded."             Else                 lblUploadStatus.Text = "Max. x files allowed."             End If         Else             lblUploadStatus.Text = "No files selected."         Stop If     End Sub End Class

For single file upload but add the beneath code in the Push click event.

fileUpload.SaveAs(Server.MapPath("CopyFiles\" & fileUpload.FileName))

Note: "CopyFiles\" is the name of the binder at the server, in which the files will uploaded.

Browser Support:
Chrome 39.0 - Aye | FireFox 34.0 - Yes | Internet Explorer 11 - Yes | Safari - Yes

← Previous Next →

clubbdrationotled.blogspot.com

Source: https://www.encodedna.com/2013/02/multiple-file-upload-in-aspdotnet.htm

0 Response to "using multiple file uploader to upload image in asp.net c#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel