打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Asp.net实现多图片上传
前台代码----》
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUp.aspx.cs" Inherits="FileUp" %>
<!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 id="Head1" runat="server">
<title>ASP.NET(C#)实现一次性上传多张图片</title>
<script type="text/javascript">
var i=1;
function addFile()
{
    if (i<10)
    {
        var str = '<BR> <input type="file" name="File" runat="server" style="width: 200px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />';
        document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
    }
    else
    {
        alert("您一次最多只能上传10张图片!");
    }
    i++;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" align="center" border="0" cellpadding="1" cellspacing="1" class="table">
    <tr>
        <td align="center"><font color="#0000ff" face="宋体" size="3"><strong>上传图片</strong></font></td>
    </tr>
    <tr>
        <td align="center" style="width: 734px"></td>
    </tr>
    <tr>
        <td align="center" style="width: 734px">
        <asp:Panel ID="Panel5" runat="server">
        <table width="100%">
            <tr>
                <td align="right" style="width: 100px"></td>
                <td align="left" style="width: 500px">说明:点增加图片按钮可一次上传多张图片,可为每张图片写上一句不超过20个字的描述。单张图片大小不大于1024k</td>
            </tr>
            <tr>
                <td align="right" style="width: 100px">请选择图片:<br /></td>
                <td align="left" style="width: 500px"><p id="MyFile"><input onclick="addFile()" type="button" value="增加图片(Add)"></p><br /><input id="File1" type="file" name="File" runat="server" style="width: 245px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" /></td>
            </tr>
            <tr>
                <td align="right" style="width: 100px"></td>
                <td align="left" style="width: 500px"><asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click"  /></td>
            </tr>
            <tr>
                <td align="right" style="width: 100px"></td>
                <td align="left" style="width: 500px"><asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
            </tr>
        </table>
        </asp:Panel>
        </td>
    </tr>
    <tr>
        <td align="center" style="width: 734px"></td>
    </tr>
</table>
</div>
</form>
</body>
</html>
 
 
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.IO;
public partial class FileUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessage.Visible = false;
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
        string[] rd = Request.Form[1].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
        //string albumid=ddlAlbum.SelectedValue.Trim();
        int ifile;
        for (ifile = 0; ifile < files.Count; ifile++)
        {
            if (files[ifile].FileName.Length > 0)
            {
                System.Web.HttpPostedFile postedfile = files[ifile];
                if (postedfile.ContentLength / 1024 > 1024)//单个文件不能大于1024k
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>");
                    break;
                }
                string fex = Path.GetExtension(postedfile.FileName);
                if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF"&& fex !=".png"&& fex !=".PNG")
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) + "---图片格式不对,只能是jpg或gif或png<br>");
                    break;
                }
            }
        }
        if (strmsg.Length <= 0)    //说明图片大小和格式都没问题
        {
            //以下为创建图库目录
            string dirpath = Server.MapPath("photo");
            if (Directory.Exists(dirpath) == false)
            {
                Directory.CreateDirectory(dirpath);
            }
            Random ro = new Random();
            int name = 1;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile myFile = files[i];
                string FileName = "";
                string FileExtention = "";
                FileName = System.IO.Path.GetFileName(myFile.FileName);
                string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片
                string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
                if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                {
                    FileExtention = System.IO.Path.GetExtension(myFile.FileName);
                    string ppath = dirpath + @"/" + NewName + FileExtention;
                    myFile.SaveAs(ppath);
                }
                name = name + 1;//用来重命名规则的变量
            }
            Response.Write("<script>alert('恭喜,图片上传成功!')</script>");
        }
        else
        {
            lblMessage.Text = strmsg.ToString();
            lblMessage.Visible = true;
        }
    }
}


 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
无组件上传图片到数据库中,最完整解决方案
网页设计中当前正在访问的导航栏高亮显示的解决方案
asp做问卷调查 投票
微星横向菜单
天气web service网站实例讲解
透明动态的相框
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服