asp.net 批量上传图片保存到数据库

表结构:

tb_pic

ID (int)主键自增

pic (varchar(max)) 上传图片的路径

xxxx.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadImg.aspx.cs" Inherits="Web.Services.UploadImg" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>批量上传图片</title>
    <link rel="stylesheet" type="text/css" href="../Common/css/PopWinStyle.css" />
    <script src="../Common/js/PopWin.js" type="text/javascript"></script>
    <script type="text/javascript">
        var i = 1;
        function addFile() {
            if (i < 6) {
                var str = '<input type="file" name="File" runat="server" style="width: 300px"/>'
                document.getElementById('MyFile').insertAdjacentHTML("beforeEnd", str);
            }
            else {
                alert("您一次最多只能上传6张图片!");
            }
            i++;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div class="Pagetitle">
            <i class="icon-align-justify"></i>图片上传
        </div>
        <div>
            <asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click" />
            <input οnclick="addFile()" type="button" value="添加图片" /><br /> //单击动态添加选择图片路径
        </div>
        <div>
            <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>  
        </div>
        <div>
            <table id="MyFile">
                <%--<tr>
                    <td style="text-align: right; width: 80px">图片:</td>
                    <td style="text-align: left">
                        <div style="border: 1px solid #808080;">
                        <asp:Image ID="img" runat="server" Width="160px" Height="120px" οnclick="ShowPControl('图片管理', '../Sel/ImageInfo.aspx?type=1&ci=625', 536, 438, 'SetImg')" /></div>
                        <asp:HiddenField ID="hfImgID" runat="server" Value="0" />
                        <asp:HiddenField ID="hfUrl" runat="server" />
                    </td>
                </tr>--%>
                <tr >
                    <td>
                        <input type="file" name="File" style="width: 300px" />  //选择图片路径,第一次默认加载一个选择路径
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>

</html>

前台效果图:


加上自己想要的css

后台代码:

xxxx.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;
using DAL;
using System.Data.SqlClient;
using System.Data;


namespace Web.Services
{
    public partial class UploadImg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Visible = false;
        }


        /// <summary>  
        /// 上传照片  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            lblMessage.Visible = true;
            string FileName = "", PicPath = "";
            int ifile;
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
            for (ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    System.Web.HttpPostedFile postedfile = files[ifile];
                    if (postedfile.ContentLength / 1024 > 512)//单个文件不能大于512k  
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "不能大于512k!<br>");
                        break;
                    }
                    string fex = Path.GetExtension(postedfile.FileName);
                    if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF" && fex != ".bmp" && fex != ".BMP")
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "图片格式仅支持jpg,gif,bmp!<br>");
                        break;
                    }
                }
                else
                {
                    strmsg.Append("没有图片!");
                    break;
                }
            }
            if (strmsg.Length <= 0)//图片大小和格式都没问题  
            {
                //创建图库目录  
                string dirname = "pic";
                string dirpath = Server.MapPath("/");
                dirpath = dirpath + @"/" + dirname;
                if (Directory.Exists(dirpath) == false)
                {
                    Directory.CreateDirectory(dirpath);
                }
                for (int i = 0; i < files.Count; i++)
                {
                    System.Web.HttpPostedFile myFile = files[i];
                    FileName = System.IO.Path.GetFileName(files[i].FileName);
                    if (FileName.Length > 0)//存在上传文件  
                    {
                        string ppath = dirpath + @"/" + FileName;
                        myFile.SaveAs(ppath);
                        PicPath += FileName + "|";//拼接图片目录  
                    }
                }
                AddPicture(PicPath);
            }
            else
            {
                lblMessage.Text = strmsg.ToString();
            }
        }




        /// <summary>  
        /// 将图片路径保存到数据库  
        /// </summary>  
        /// <param name="imgpath"></param>  
        private void AddPicture(string imgpath)
        {
            string sql = "insert into " + SqlDataBase.DataBase1 + @"tb_pic values('" + imgpath + "')";  //这些事框架自带,用自己的方法实现即可
            int result = SqlHelp.ExecuteSql(new SqlConnection(), new SqlCommand(), sql); //这些事框架自带,用自己的方法实现即可
            if (result > 0)
            {
                lblMessage.Text = "成功!";
            }
            else
            {
                lblMessage.Text = "失败!";
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值