How to implement multi-file uploading in asp.net-practical skills

Source: Internet
Author: User
Tags file upload httpcontext
In previous Web applications, uploading files was a hassle, and now with. NET, file uploads have become a breeze. The following example implements the multiple file upload function. You can dynamically add input forms, and the number of uploaded files is unlimited. The code is as follows:

Multiupload.aspx

<%@ Page language= "vb" autoeventwireup= "false" codebehind= "MultiUpload.aspx.vb"
inherits= "Aspxweb.multiupload"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> multi-File upload </title>
<script language= "JavaScript" >
function AddFile ()
{
var str = ' <input type= "file" size= "name=" file ">"
document.getElementById (' MyFile '). insertAdjacentHTML ("BeforeEnd", str)
}
</script>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "server" enctype= "Multipart/form-data" >
<center>
<asp:label runat= "Server" id= "MyTitle" ></asp:Label>
<p id= "MyFile" ><input type= "file" size= "$" name= "file" ></P>
<P>
<input type= "button" value= "Add" onclick= "AddFile ()" >
<asp:button runat= "Server" text= "upload" id= "Upload" ></asp:Button>
<input onclick= "This.form.reset ()" type= "button" value= "Reset" >
</P>
</center>
<p align= "center" >
<asp:label id= "strstatus" runat= "server" font-names= "Arial" font-bold= "True"
Font-size= "9pt" width= "500px" borderstyle= "None" bordercolor= "white" ></asp:Label>
</P>
</form>
</body>
</HTML>
Post code: MultiUpload.aspx.vb

Public Class Multiupload
Inherits System.Web.UI.Page
Protected WithEvents Upload as System.Web.UI.WebControls.Button
Protected WithEvents MyTitle as System.Web.UI.WebControls.Label
Protected WithEvents strstatus as System.Web.UI.WebControls.Label

#Region "Web Form Designer generated Code"

' This are required by the Web Form Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' Codegen:this Method-required by the Web Form Designer
' Do not modify it using the ' Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Mytitle.text = "Upload.text = "Start Uploading"
If (Me.ispostback) Then me.saveimages ()
End Sub

Private Function saveimages () as System.Boolean
' Traverse the file Form element
Dim files as System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files

' Status information
Dim STRMSG as New System.Text.StringBuilder ("uploaded files are: Dim IFile as System.Int32
Try
For ifile = 0 to files. Count-1
' Check file extension name
Dim PostedFile as System.Web.HttpPostedFile = Files (ifile)
Dim FileName, fileextension as System.String
FileName = System.IO.Path.GetFileName (postedfile.filename)
If not (fileName = String.Empty) Then
FileExtension = System.IO.Path.GetExtension (fileName)
Strmsg.append ("uploaded file type:" + postedFile.ContentType.ToString () + "<br>")
Strmsg.append ("Client file Address:" + postedfile.filename + "<br>")
Strmsg.append ("FileName of uploaded file:" + filename + "<br>")
Strmsg.append ("Upload file extension:" + fileextension + "<br>' Can be saved to a different folder depending on the extended name
Postedfile.saveas (System.Web.HttpContext.Current.Request.MapPath ("images/") + fileName)
End If
Next
Strstatus.text = strmsg.tostring ()
Return True
Catch Ex as System.Exception
Strstatus.text = Ex.message
Return False
End Try
End Function
End Class
C # version

Upload.aspx

<%@ Page language= "C #" codebehind= "UpLoad.aspx.cs" autoeventwireup= "false" inherits= "Webportal.upload"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> multi-File upload </title>
<script language= "JavaScript" >
function AddFile ()
{
var str = ' <input type= "file" size= "name=" file ">"
document.getElementById (' MyFile '). insertAdjacentHTML ("BeforeEnd", str)
}
</script>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "server" enctype= "Multipart/form-data" >
<div align= "center" >
<p id= "MyFile" ><input type= "file" size= "$" name= "file" ></P>
<P>
<input type= "button" value= "Add" onclick= "AddFile ()" >
<input onclick= "This.form.reset ()" type= "button" value= "Reset" >
<asp:button runat= "Server" text= "Start uploading" id= "Uploadbutton" ></asp:Button>
</P>
<P>
<asp:label id= "strstatus" runat= "server" font-names= "Arial" font-bold= "True" font-size= "9pt"
Width= "500px" borderstyle= "None" bordercolor= "white" ></asp:Label>
</P>
</div>
</form>
</body>
</HTML>
UpLoad.aspx.cs

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

Namespace Webportal
{
<summary>
Summary description of the UpLoad.
Implementing multiple file Uploads
</summary>
public class Upload:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Uploadbutton;
protected System.Web.UI.WebControls.Label strstatus;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (this. IsPostBack) this. Saveimages ();
}

Private Boolean saveimages ()
{
' Traverse the file Form element
httpfilecollection files = HttpContext.Current.Request.Files;

' Status information
System.Text.StringBuilder STRMSG = new System.Text.StringBuilder ();
Strmsg.append ("uploaded files are: Try
{
for (int ifile = 0; ifile < files. Count; ifile++)
{
' Check file extension name
Httppostedfile postedfile = Files[ifile];
String FileName, FileExtension;
FileName = System.IO.Path.GetFileName (postedfile.filename);
if (FileName!= "")
{
FileExtension = System.IO.Path.GetExtension (fileName);
Strmsg.append ("uploaded file type:" + postedFile.ContentType.ToString () + "<br>");
Strmsg.append ("Client file Address:" + postedfile.filename + "<br>");
Strmsg.append ("FileName of uploaded file:" + filename + "<br>");
Strmsg.append ("Upload file extension:" + fileextension + "<br>' Can be saved to a different folder depending on the extended name
Note: You may want to modify the anonymous write permission for your folder.
Postedfile.saveas (System.Web.HttpContext.Current.Request.MapPath ("images/") + FileName);
}
}
Strstatus.text = Strmsg.tostring ();
return true;
}
catch (System.Exception Ex)
{
Strstatus.text = Ex.message;
return false;
}
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.id = "Upload";
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.