实现用户自定义文件个数
实现文件过滤
实现文件的安日期存放
演示文档仅提供技术参考,实际开发中还有很多需要优化。
FileUp.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUp.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="javascript">
function ChangFile()
{
FileList.innerHTML="";
for(var i=1;i<=parseInt(form1.FileNum.value);i++)
{
FileList.innerHTML+="<input name='File' type='file' /><br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>多图片上传</title>
</head>
<body>
<form id="form1" runat="server">
<br />选择图片个数
<select id="FileNum" name="FileNum" onchange="ChangFile()" >
<option value=1 >1</option>
<option selected="selected" value=2 >2</option>
<option value=3 >3</option>
<option value=4 >4</option>
<option value=5 >5</option>
<option value=6 >6</option>
</select>
<div id="FileList">
<input id="File1" type="file" runat=server /><br />
<input id="File2" type="file" runat=server />
<br />
<input id="File3" type="file" runat=server />
</div>
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
</div>
</form>
</body>
</html>
FileUp.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
/// <summary>
/// 多图片上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string FilePath = @Server.MapPath("~/") + DateTime.Now.ToString("yyMMdd");
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
string FileName = DateTime.Now.ToString("hhmmss");
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength > 0)
{
string FileType=GetFileType(Request.Files[i].FileName);
if(TypeFilter(FileType))
{
string @Filename = FilePath + "//" + FileName + "_" + i.ToString() + FileType;
Request.Files[i].SaveAs(@Filename);
Response.Write(@Filename+"<br>");
}
}
}
}
private string GetFileType(string Filename)
{
string Type = "";
Type = Filename.Substring(Filename.LastIndexOf('.'));
return Type;
}
private bool TypeFilter(string type)
{
string FileType = ".gif.jpg.swf"; //开发中从配置文件中读取
FileType = FileType.ToLower();
if (FileType.IndexOf(type.ToLower())>-1)
{
return true;
}
else
{
return false;
}
}
}