FileUpload 文件上传 多文件上传

aspx控件:FileUpload

<asp:FileUpload ID="FileUpload1" runat="server" />

如果要一次上传多个文件 控件属性设置:AllowMultiple="True"

代码:

如果.NET Framework为4.5及以上的  可以使用“GetMultiple”方法

HttpFileCollection hc = HttpContext.Current.Request.Files;
var files = hc.GetMultiple("FileUpload1");
foreach (HttpPostedFile item in files)
{

}

如果是4.5以下的

HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
if (hfc.Count > 0)
{
    if (hfc[0].ContentLength < 1000000)
    {
        string ImgExtend = getExtend(hfc[0].FileName);
        if (ImgExtend == ".bmp" || ImgExtend == ".jpg" || ImgExtend == ".gif" || ImgExtend == ".png")
        {
                                string path = "UpFiles\\WebPageImage\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + FromUserName;
                                if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/") + path) == false)
                                {
                                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/") + path);
                                }

                                content = DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + getExtend(hfc[0].FileName);

                                savepath = System.Web.HttpContext.Current.Server.MapPath("/") + path + "\\" + content;
                                file = "\\" + path + "\\" + content;
                                Session["ImgPath"] = file;
                                hfc[0].SaveAs(savepath);
                                str = "上传成功";
                            }
                            else if (ImgExtend == "")
                            { }
                            else
                            {
                                str = "上传的图片格式只能是jpg、png!";
                            }
                        }
                        else
                        { str = "上传的图片大小为1M以下!"; }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值