asp.net html控件的File控件实现多文件上传实例分享
程序员文章站
2024-03-05 09:17:18
asp.net多文件上传使用html控件的file控件,在form中就需要加入【 enctype="multipart/form-data"】。 up3.aspx文件代码...
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 + "】";
}
}
上一篇: 安卓(Android)ListView 显示图片文字
下一篇: PHP 芝麻信用接入的注意事项