asp.net html控件的File控件實(shí)現(xiàn)多文件上傳實(shí)例分享
up3.aspx文件代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up3.aspx.cs" Inherits="up3" %>
<!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></title>
<script language="javascript" type="text/javascript">
function addFile() {
var odiv = document.getElementById("MyFile");
var str = "<div><input name='File' type='file' /></div>";
odiv.insertAdjacentHTML("beforeEnd", str);
}
function resetFile() {
var odiv = document.getElementById("MyFile");
odiv.innerHTML = "<div><input name='File' type='file' /></div>";
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="button" value="增加" onclick="addFile()" />
<input type="button" value="重置" onclick="resetFile()" />
<div id="MyFile">
<div><input name="File" type="file" /></div>
</div>
<asp:Button runat="server" Text="上傳" ID="Button1" OnClick="Button1_Click" BorderColor="Desktop"
BorderWidth="1px" Height="20px" Width="60px"></asp:Button>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
up3.aspx.cs文件代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class up3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string upPath = "/up/"; //上傳文件路徑
int upLength = 5; //上傳文件大小
string upFileExtName = "|bmp|jpg|jpeg|png|gif|";
HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
int flag = _files.Count;
int flagN = 0;
int flagE = 0;
int flagEE = 0;
string flagEEstr = "";
for (int i = 0; i < _files.Count; i++)
{
string name = _files[i].FileName;
FileInfo fi = new FileInfo(name);
string oldfilename = fi.Name;
string scExtension = fi.Extension.ToLower();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + fi.Extension; // 文件名稱,當(dāng)前時間(yyyyMMddhhmmssfff)
string webFilePath = Server.MapPath(upPath) + fileName; // 服務(wù)器端文件路徑
if (upFileExtName.IndexOf(scExtension.Replace(".", "")) == -1)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個文件,文件名[" + oldfilename + "],文件類型不符合!";
continue;
}
if ((fi.Length / (1024 * 1024)) > upLength)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個文件,文件名[" + oldfilename + "],超出" + upLength + "M大小限制!";
continue;
}
try
{
_files[i].SaveAs(webFilePath);
}
catch (Exception ex)
{
flagEE = flagEE + 1;
flagEEstr = flagEEstr + "第" + (i + 1) + "個文件,上傳異?!?+ex.Message+"】";
}
}
Label1.Text = "總文件【" + flag + "】,上傳成功文件【" + flagN + "】,異常文件【" + (flagE + flagEE) + "】【" + flagEEstr + "】";
}
}
相關(guān)文章
如何在ASP.NET Core 的任意類中注入Configuration
這篇文章主要介紹了如何在 ASP.NET Core 的任意類中注入Configuration ,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04ASP.NET網(wǎng)站導(dǎo)航及導(dǎo)航控件如何使用
這篇文章主要介紹了ASP.NET網(wǎng)站導(dǎo)航及導(dǎo)航控件如何使用,需要的朋友可以參考下2015-09-09關(guān)于多對多關(guān)系表無法更新與插入的問題
這篇文章主要介紹了關(guān)于多對多關(guān)系表無法更新與插入的問題 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07一個簡單的ASP.NET Forms 身份認(rèn)證的實(shí)例方法
當(dāng)訪問默認(rèn)首頁default.aspx時,會自動跳轉(zhuǎn)到login.aspx頁面上請求登錄,隨便輸入用戶名和密碼,點(diǎn)擊“登錄”按鈕,會回到首頁,并顯示當(dāng)前登錄的用戶名。2013-07-07ASP.NET?Core實(shí)現(xiàn)動態(tài)獲取文件并下載
這篇文章介紹了ASP.NET?Core實(shí)現(xiàn)動態(tài)獲取文件并下載的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01C#反射技術(shù)的簡單操作(讀取和設(shè)置類的屬性)
反射的作用想必大家都知道了吧,少量屬性的自動化操作手動添加幾下當(dāng)然是沒有問題的,但是屬性數(shù)量較多的時候敲起這些繁鎖的代碼可以困了,再說對擴(kuò)展和維護(hù)性造成很多的不遍,以下代碼中如不能直接使用請?zhí)砑觰sing System.Text;的引用。2011-01-01告別ADO.NET實(shí)現(xiàn)應(yīng)用系統(tǒng)無縫切換的煩惱(總結(jié)篇)
說起ADO.NET,就扯上了數(shù)據(jù)庫訪問類庫了,現(xiàn)在的每個項(xiàng)目的數(shù)據(jù)庫訪問類應(yīng)該說都很強(qiáng)的了,經(jīng)常就聽到說我的我們的數(shù)據(jù)庫訪問類怎么怎么強(qiáng)大而且支持多數(shù)據(jù)庫,現(xiàn)在的大家做的項(xiàng)目里用的數(shù)據(jù)庫訪問類庫我想也都是支持多數(shù)據(jù)庫吧,支持到什么程度我就不知道了2009-11-11HttpWebRequest的常見錯誤使用TcpClient可避免
有時使用HttpWebRequest對象會出現(xiàn)錯誤有三種服務(wù)器提交了協(xié)議沖突/基礎(chǔ)連接已經(jīng)關(guān)閉:連接被意外關(guān)閉/無法發(fā)送具有此謂詞類型的內(nèi)容正文,感興趣的朋友可以參考下本文2013-02-02詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板
這篇文章主要介紹了詳解使用DotNet CLI創(chuàng)建自定義的WPF項(xiàng)目模板,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04