asp. net file upload solution (picture upload single file upload multi file upload check file type)

  • 2021-07-01 07:20:24
  • OfStack

This site also introduced a number of ASP. NET file upload solutions, today a large collection of asp. net file upload.

1 Upload pictures using standard HTML
Foreground code:


<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2" style="height: 21px" > 
      Usage standard HTML To upload pictures </td> 
   </tr> 
   <tr> 
    <td style="width: 400px"> 
     <input id="InputFile" style="width: 399px" type="file" runat="server" /></td> 
    <td style="width: 80px"> 
     <asp:Button ID="UploadButton" runat="server" Text=" Upload pictures " OnClick="UploadButton_Click" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" > 
     <asp:Label ID="Lb_Info" runat="server" ForeColor="Red"></asp:Label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body>


Background code:


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 

 } 
 protected void UploadButton_Click(object sender, EventArgs e) 
 { 
  string uploadName = InputFile.Value;// Get the full path of the picture to be uploaded, including the file name  
  //string uploadName = InputFile.PostedFile.FileName; 
  string pictureName = "";// After uploading the picture name, take the current time as the file name, make sure that the file name is not duplicate  
  if (InputFile.Value != "") 
  { 
   int idx = uploadName.LastIndexOf("."); 
   string suffix = uploadName.Substring(idx);// Get the suffix of the uploaded picture  
   pictureName = DateTime.Now.Ticks.ToString() + suffix; 
  } 
  try 
  { 
   if (uploadName != "") 
   { 
    string path = Server.MapPath("~/images/"); 
    InputFile.PostedFile.SaveAs(path + pictureName); 
   } 
  } 
  catch (Exception ex) 
  { 
   Response.Write(ex); 
  } 
 } 
}

2 Single File Upload
This is the most basic file upload, in asp. net1.x does not have this FileUpload control, only html upload control, at that time to convert html control into server control, very difficult to use. In fact, all the beautiful effects of uploading files are derived from this FileUpload control. Although the first example is simple, it is fundamental.
Foreground code:


<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table style="width: 90%"> 
   <tr> 
    <td style="width: 159px" colspan=2> 
     <strong><span style="font-size: 10pt"> The simplest single file upload </span></strong></td> 
   </tr> 
   <tr> 
    <td style="width: 600px"> 
     <asp:FileUpload ID="FileUpload1" runat="server" Width="600px" /></td> 
    <td align=left> 
     <asp:Button ID="FileUpload_Button" runat="server" Text=" Upload pictures " OnClick="FileUpload_Button_Click" /></td> 
   </tr> 
   <tr> 
    <td colspan=2> 
     <asp:Label ID="Upload_info" runat="server" ForeColor="Red" Width="767px"></asp:Label></td> 
   </tr> 
  </table>  
 </div> 
 </form> 
</body>

Background code:


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 

 } 
 protected void FileUpload_Button_Click(object sender, EventArgs e) 
 { 
  try 
  { 
   if (FileUpload1.PostedFile.FileName == "") 
   //if (FileUpload1.FileName == "") 
   //if (!FileUpload1.HasFile)  // Get 1 A value indicating that  System.Web.UI.WebControls.FileUpload  Control contains a file. Include a file; Otherwise,  true ; Otherwise; Otherwise  false .  
   { 
    this.Upload_info.Text = " Please select Upload File! "; 
   } 
   else 
   { 
    string filepath = FileUpload1.PostedFile.FileName; // What you get is the full path of the file , Include file names, such as: C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
    //string filepath = FileUpload1.FileName;    // Get the uploaded file name 20022775_m.jpg 
    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg 
    string serverpath = Server.MapPath("~/images/") + filename;// Get the location where the file is saved on the server C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
    FileUpload1.PostedFile.SaveAs(serverpath);// Save the uploaded file as  
    this.Upload_info.Text = " Upload successfully! "; 
   } 
  } 
  catch (Exception ex) 
  { 
   this.Upload_info.Text = " Upload error! The reason is: " + ex.ToString(); 
  } 
 } 
}


3. Upload multiple files
Foreground code:


<body> 
 <form id="form1" runat="server"> 
 <div> 
 <table style="width: 343px"> 
   <tr> 
    <td style="width: 100px"> 
      Multiple file upload </td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:FileUpload ID="FileUpload1" runat="server" Width="475px" /> 
     </td> 
    <td style="width: 100px"> 
     </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:FileUpload ID="FileUpload2" runat="server" Width="475px" /></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:FileUpload ID="FileUpload3" runat="server" Width="475px" /></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
   <tr> 
    <td style="width: 100px"> 
     <asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="1 Upload " /> 
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="448px"></asp:Label></td> 
    <td style="width: 100px"> 
    </td> 
   </tr> 
  </table> 
 </div> 
 </form> 
</body>


Background code:


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 

 } 
 protected void bt_upload_Click(object sender, EventArgs e) 
 { 
  if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "") 
  { 
   this.lb_info.Text = " Please select a file! "; 
  } 
  else 
  { 
   HttpFileCollection myfiles = Request.Files; 
   for (int i = 0; i < myfiles.Count; i++) 
   { 
    HttpPostedFile mypost = myfiles[i]; 
    try 
    { 
     if (mypost.ContentLength > 0) 
     { 
      string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
      string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg 
      string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg 
      mypost.SaveAs(serverpath); 
      this.lb_info.Text = " Upload successfully! "; 
     } 
    } 
    catch (Exception ex) 
    { 
     this.lb_info.Text = " Upload error! Reason: " + ex.Message.ToString(); 
    } 
   } 
  } 
 } 
}

4. The client checks the uploaded file type (take the uploaded picture as an example)


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
 <title> Client checks uploaded file type </title> 
 <script language="javascript"> 
 function Check_FileType() 
 { 
  var str=document.getElementById("FileUpload1").value; 
  var pos=str.lastIndexOf("."); 
  var lastname=str.substring(pos,str.length); 
  if(lastname.toLowerCase()!=".jpg"&&lastname.toLowerCase()!=".gif") 
  { 
   alert(" The file type you uploaded is "+lastname+" The picture must be .jpg,.gif Type "); 
   return false; 
  } 
  else 
  { 
   return true; 
  }   
 } 
 </script> 
</head> 
<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2"> 
      Client checks uploaded file type </td>     
   </tr> 
   <tr> 
    <td style="width: 444px"> 
     <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td> 
    <td style="width: 80px"> 
     <asp:Button ID="bt_upload" runat="server" Text=" Upload pictures " OnClick="bt_upload_Click" OnClientClick="return Check_FileType()" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" style="height: 21px"> 
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body> 
</html>

Note: Client event OnClientClick= "return Check_FileType ()" is triggered when you click Upload


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 

 } 

 protected void bt_upload_Click(object sender, EventArgs e) 
 { 
  try 
  { 
   if (FileUpload1.PostedFile.FileName == "") 
   { 
    this.lb_info.Text = " Please select a file! "; 
   } 
   else 
   { 
    string filepath = FileUpload1.PostedFile.FileName; 
    //if (!IsAllowedExtension(FileUpload1)) 
    //{ 
    // this.lb_info.Text = " The uploaded file format is incorrect! "; 
    //} 
    if (IsAllowedExtension(FileUpload1) == true) 
    { 
     string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1); 
     string serverpath = Server.MapPath("~/images/") + filename; 
     FileUpload1.PostedFile.SaveAs(serverpath); 
     this.lb_info.Text = " Upload successfully! "; 
    } 
    else 
    { 
     this.lb_info.Text = " Please upload pictures! "; 
    } 
   } 
  } 
  catch (Exception ex) 
  { 
   this.lb_info.Text = " Upload error! Reason: " + ex.ToString(); 
  } 
 } 
 private static bool IsAllowedExtension(FileUpload upfile) 
 { 
  string strOldFilePath = ""; 
  string strExtension=""; 
  string[] arrExtension ={ ".gif", ".jpg", ".bmp", ".png" }; 
  if (upfile.PostedFile.FileName != string.Empty) 
  { 
   strOldFilePath = upfile.PostedFile.FileName;// Get the full pathname of the file  
   strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));// Get the file extension, such as: .jpg 
   for (int i = 0; i < arrExtension.Length; i++) 
   { 
    if (strExtension.Equals(arrExtension[i])) 
    { 
     return true; 
    } 
   } 
  } 
  return false; 
 } 
}

Note: If the client script and the client event OnClientClick= "return Check_FileType ()" are removed, the background code
Replace with the following:


if (!IsAllowedExtension(FileUpload1)) 
    { 
     this.lb_info.Text = " The uploaded file format is incorrect! "; 
    } 


else if (IsAllowedExtension(FileUpload1) == true)
That is, the server side checks the uploaded file type.
5. The server side checks the type of uploaded file (the real format inside the file)


<body> 
 <form id="form1" runat="server"> 
 <div> 
  <table> 
   <tr> 
    <td colspan="2"> 
      The server checks the uploaded file type </td>     
   </tr> 
   <tr> 
    <td style="width: 444px"> 
     <asp:FileUpload ID="FileUpload1" runat="server" Width="432px" /></td> 
    <td style="width: 80px"> 
     <asp:Button ID="bt_upload" runat="server" Text=" Upload pictures " OnClick="bt_upload_Click" /></td> 
   </tr> 
   <tr> 
    <td colspan="2" style="height: 21px"> 
     <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="515px"></asp:Label></td>     
   </tr> 
  </table>  
 </div> 
 </form> 
</body>

Background code:


using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
 protected void Page_Load(object sender, EventArgs e) 
 { 

 } 
 protected void UploadButton_Click(object sender, EventArgs e) 
 { 
  string uploadName = InputFile.Value;// Get the full path of the picture to be uploaded, including the file name  
  //string uploadName = InputFile.PostedFile.FileName; 
  string pictureName = "";// After uploading the picture name, take the current time as the file name, make sure that the file name is not duplicate  
  if (InputFile.Value != "") 
  { 
   int idx = uploadName.LastIndexOf("."); 
   string suffix = uploadName.Substring(idx);// Get the suffix of the uploaded picture  
   pictureName = DateTime.Now.Ticks.ToString() + suffix; 
  } 
  try 
  { 
   if (uploadName != "") 
   { 
    string path = Server.MapPath("~/images/"); 
    InputFile.PostedFile.SaveAs(path + pictureName); 
   } 
  } 
  catch (Exception ex) 
  { 
   Response.Write(ex); 
  } 
 } 
}
0

Recommend a topic for everyone to learn: "ASP. NET file upload summary"

Is the content very wonderful, like friends to collect it, in the future encountered ASP. NET file upload problems can be helpful.

Related articles: