aspx file upload and download, multi-File upload

Source: Internet
Author: User

<%@ page language= "C #" autoeventwireup= "true" codefile= "MultiFileUpload.aspx.cs"
inherits= "Multifileupload"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<title> dimant Multi-file upload test </title>

<script type= "Text/javascript" >
function AddFile () {
var div = document.createelement ("div");
var f = document.createelement ("input");
F.setattribute ("type", "File")
F.setattribute ("name", "File")
F.setattribute ("Size", "50")
Div.appendchild (f)
var d = document.createelement ("input");
D.setattribute ("Type", "button")
D.setattribute ("onclick", "detefile (This)");
D.setattribute ("Value", "Remove")
Div.appendchild (d)
document.getElementById ("_container"). AppendChild (Div);
}

function Detefile (o) {
while (o.tagname! = "DIV") o = O.parentnode;
O.parentnode.removechild (o);
}
</script>

<body>
<form id= "Form1" runat= "Server" method= "POST" enctype= "Multipart/form-data" >
User name: <asp:textboxid= "TextBox1" runat= "Server" ></asp:TextBox>
<div id= "_container" >
<input type= "File" size= "name=" "File"/>
</div>
<div>
<input type= "button" value= "Add File (Add)" onclick= "AddFile ()"/>
</div>
<div style= "padding:10px 0" >
<asp:buttonrunat= "Server" text= "Start uploading" id= "Uploadbutton"
onclick= "Uploadbutton_click" ></asp:Button>
</div>
<div>
<asp:label id= "strstatus" runat= "server" font-names= "Arial" font-bold= "True" font-size= "9pt"
Width= "500px" borderstyle= "None" bordercolor= "white" ></asp:Label>
</div>
</form>
</body>

C # code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Publicpartialclass MultiFileUpload:System.Web.UI.Page
{
Protectedvoid Uploadbutton_click (object sender, EventArgs e)
{
' Traverse file Form element
httpfilecollection files = HttpContext.Current.Request.Files;

' Status information
System.Text.StringBuilder STRMSG = new System.Text.StringBuilder ("The user name you entered is:" + TextBox1.Text + "<br/>");
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 ("uploaded file extension:" + fileextension + "<br>' Can be saved to a different folder depending on the extension 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 ();
}
catch (System.Exception Ex)
{
Strstatus.text = Ex.message;
}

}
}

Download

public void Download (string path)//name of the file to download
{such as String path = "\\images\\a.text"; check its own path problem, some need to add a parent directory, some direct input file name can be
if (!string.  IsNullOrEmpty (Path)) {string filePath = Server.MapPath ("") +path;//Path// String FilePath[email protected]"C:\Users\Public\Pictures\Sample pictures\1.jpg";           FileInfo FileInfo = new FileInfo (filePath);                     response.clear ();                            Response.clearcontent ();                       response.clearheaders ();                           Response.AddHeader ("Content-disposition", "attachment;filename=" + fileinfo.name);                Response.AddHeader ("Content-length", fileInfo.Length.ToString () );                               Response.AddHeader ("content-transfer-encoding", "binary");                             Response.ContentType = "Application/octet-stream";                                response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");               Response.WriteFile (fileinfo.fullname);                          Response.Flush ();                          Response.End ();                    }
} 

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.