實(shí)現(xiàn)ASP.NET多文件上傳程序代碼
更新時(shí)間:2008年03月14日 19:27:17 作者:
ASP.NET多文件上傳程序
upload.aspx
復(fù)制代碼 代碼如下:
<%@ 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>多文件上傳</title>
<script language="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" 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">
<h3>多文件上傳</h3>
<P id="MyFile"><INPUT type="file" size="50" 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="開始上傳" ID="UploadButton"></asp:Button>
</P>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋體" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</P>
</div>
</form>
</body>
</HTML>
upload.aspx.cs
復(fù)制代碼 代碼如下:
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>
/// UpLoad 的摘要說明。
/// 實(shí)現(xiàn)多文件上傳
/// </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)
{
/// 在此處放置用戶代碼以初始化頁面
if (this.IsPostBack) this.SaveImages();
}
private Boolean SaveImages()
{
///'遍歷File表單元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '狀態(tài)信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上傳的文件分別是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'檢查文件擴(kuò)展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上傳的文件類型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客戶端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上傳文件的文件名:" + fileName + "<br>");
strMsg.Append("上傳文件的擴(kuò)展名:" + fileExtension + "<br><hr>");
///'可根據(jù)擴(kuò)展名字的不同保存到不同的文件夾
///注意:可能要修改你的文件夾的匿名寫入權(quán)限。
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;
}
}
#region Web 窗體設(shè)計(jì)器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
相關(guān)文章
ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)
這篇文章介紹了ASP.NET延遲調(diào)用或多次調(diào)用第三方Web?API服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10asp.net 頁面間傳值與跳轉(zhuǎn)的區(qū)別
通過Server.Transfer("b.aspx") 與Response.Redirect("b.aspx")的區(qū)別2010-04-04ASP.NET在IIS上注冊(cè)報(bào)0x800702e4錯(cuò)誤解決方法
報(bào)一個(gè)0x800702e4 請(qǐng)求的操作需要提升的錯(cuò)誤。解決的方法和前面大同小異,給這個(gè)aspnet_regiis.exe創(chuàng)建一個(gè)快捷方式,給它的目標(biāo)后面加上 一個(gè)-i,再右擊這個(gè)快捷方式,以管理員身份運(yùn)行,就行了2012-08-08ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建關(guān)系模型的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04asp.net 相關(guān)文章實(shí)現(xiàn)方法
大家或許會(huì)覺得很驚訝:為什么靈感之源會(huì)討論SQL?或許應(yīng)該這樣說吧:搞業(yè)務(wù)系統(tǒng),不跟SQL扯上關(guān)系似乎比較難。2009-05-05得到真實(shí)外網(wǎng)IP、IP所在國(guó)家、省份、地區(qū)(小偷程序)
通過正則抓取IP顯示網(wǎng)站中的數(shù)據(jù),并篩選出所需要的內(nèi)容即可。2011-11-11.NET?Core使用Worker?Service創(chuàng)建服務(wù)
這篇文章介紹了.NET?Core使用Worker?Service創(chuàng)建服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02