Asp.Net中同时上传多个文件

本文介绍了一个多图片上传功能的实现方式,允许用户自定义上传图片的数量,并通过JavaScript动态生成相应的文件输入框。上传后的图片会按日期存放在指定路径下,同时具备文件类型过滤功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现用户自定义文件个数
实现文件过滤
实现文件的安日期存放
演示文档仅提供技术参考,实际开发中还有很多需要优化。

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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值