极乐门资源网 Design By www.ioogu.com
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 + "】";
}
}
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 + "】";
}
}
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无asp.net html控件的File控件实现多文件上传实例分享的评论...
更新日志
2025年01月27日
2025年01月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]