程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net html控件的File控件實現多文件上傳實例分享

asp.net html控件的File控件實現多文件上傳實例分享

編輯:ASP.NET基礎
asp.net多文件上傳使用html控件的File控件,在form中就需要加入【 enctype="multipart/form-data"】。
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; // 文件名稱,當前時間(yyyyMMddhhmmssfff)
string webFilePath = Server.MapPath(upPath) + fileName; // 服務器端文件路徑
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 + "】";
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved