ASP.NET 多张图片上传,并可以预览。

一、首先先上效果图:

1、上传前

2、上传后

二、主要使用控件:FileUpload,DataList展示多张图片

三、代码

1、前台aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductZiZhiUpload.aspx.cs" Inherits="ProductZiZhiUpload" %>

<!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>
        <meta charset="UTF-8">
        
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #upBox{
                text-align: center;
                /*width:500px;*/
                padding: 2px;
                border: 1px solid #666;
                margin: auto;
                margin-top: 15px;
                margin-bottom: 20px;
                position: relative;
                border-radius: 10px;
            }
                #upBox2{
                text-align: center;
                /*width:500px;*/
                padding: 2px;
                border: 1px solid #666;
                margin: auto;
                margin-top: 15px;
                margin-bottom: 20px;
                position: relative;
                border-radius: 10px;
            }
                      #upBox3{
                text-align: center;
                /*width:500px;*/
                padding: 2px;
                border: 1px solid #666;
                margin: auto;
                margin-top: 15px;
                margin-bottom: 20px;
                position: relative;
                border-radius: 10px;
            }
                   #upBox4{
                text-align: center;
                /*width:500px;*/
                padding: 2px;
                border: 1px solid #666;
                margin: auto;
                margin-top: 15px;
                margin-bottom: 20px;
                position: relative;
                border-radius: 10px;
            }
            #inputBox{
                width: 100%;
                height: 40px;
                border: 1px solid cornflowerblue;
                color:red;
                border-radius: 20px;
                position: relative;
                text-align: center;
                line-height: 40px;
                overflow: hidden;
                font-size: 16px;
                background-color:deepskyblue;
            }
            #inputBox2{
                width: 100%;
                height: 40px;
                border: 1px solid cornflowerblue;
                color:red;
                border-radius: 20px;
                position: relative;
                text-align: center;
                line-height: 40px;
                overflow: hidden;
                font-size: 16px;
                background-color:deepskyblue;
            }
               #inputBox3{
                width: 100%;
                height: 40px;
                border: 1px solid cornflowerblue;
                color:red;
                border-radius: 20px;
                position: relative;
                text-align: center;
                line-height: 40px;
                overflow: hidden;
                font-size: 16px;
                background-color:deepskyblue;
            }
                  #inputBox4{
                width: 100%;
                height: 40px;
                border: 1px solid cornflowerblue;
                color:red;
                border-radius: 20px;
                position: relative;
                text-align: center;
                line-height: 40px;
                overflow: hidden;
                font-size: 16px;
                background-color:deepskyblue;
            }
            #inputBox input{
                width: 114%;
                height: 40px;
                opacity: 0;
                cursor: pointer;
                position: absolute;
                top: 0;
                left: -14%;
                
            }
                #inputBox2 input{
                width: 114%;
                height: 40px;
                opacity: 0;
                cursor: pointer;
                position: absolute;
                top: 0;
                left: -14%;
                
            }
                    #inputBox3 input{
                width: 114%;
                height: 40px;
                opacity: 0;
                cursor: pointer;
                position: absolute;
                top: 0;
                left: -14%;
                
            }
                    #inputBox4 input{
                width: 114%;
                height: 40px;
                opacity: 0;
                cursor: pointer;
                position: absolute;
                top: 0;
                left: -14%;
                
            }
            #imgBox{
                text-align: left;
            }
            #imgBox2{
                text-align: left;
            }
            #imgBox3{
                text-align: left;
            }
            #imgBox4{
                text-align: left;
            }
            
        .yincang{

            display:none;

        }
        #Button1{
                display: inline-block;
                text-align: center;
                line-height: 30px;
                outline: none;
                width: 100px;
                height: 30px;
                background: cornflowerblue;
                border: 1px solid cornflowerblue;
                color: white;
                cursor: pointer;
                margin-top: 30px;
                border-radius: 5px;
            }
        
         .card-layerBg {
    display: none;
    width: 100%;
    z-index: 999;
    position: fixed;
    background-color: #000;
    opacity: 0.5;
    top: 0;
    height: 100%;
    filter: alpha(opacity=50);
}

.card-layerCon {
    display: none;
    width: 600px;
    padding: 10px 30px 20px 0;
    background-color: #fff;
    z-index: 9999;
    position: fixed;
    left: 50%;
    top: 50%;
    margin-left: -300px;
    opacity: 1;
    filter: alpha(opacity=100);
    text-align:center;
}

.close {
    font-size: 33px;
    text-align: center;
    color: #ab2223;
    position: absolute;
    right: 10px;
    top: -8px;
    cursor: pointer;
}

.card-layerM {
    width: 100%;
    margin: 0 auto;
    padding: 10px;
    padding-top: 25px;
    overflow-y: auto;
    max-height:100%;
    text-align: center;

}
.card-layerM img{
    width:auto;
    max-width:100%;
    max-height:100%;
}
        
          
        </style>
        
    <script src="js/jquery1.10.2.min.js"></script>
    <script src="My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript">

//有效期字段是否为空检验方法
        function btnsave_onclick() {

            var ctime = $("*[id$=tendtime]").val();
            $("*[id$=Label2]").text("*");
            if (ctime == "") {

                alert("有效期为空!");
                return false;

            } else {

                return true;

            }

        }

//点击上传图片查询大图的js

        function layer(small) {
            $(".layer_con img").on("load", function () {
                var $con = $(".layer_con").height() + 30;
                $(".layer_bg").css({ "display": "block" });
                var $winH = $(window).height();
                if ($con > $winH) {
                    $(".layer_con").css({ "height": $winH, "display": "block", "top": "0", "margin-top": "0" });
                } else {
                    $(".layer_con").css({ "display": "block", "top": "50%", "margin-top": -$con / 2 });
                }
            })
        }

//关闭放大图片
        $(function () {

            $(".close").on("click", function () {
                $(".layer_con").css("display", "none");
                $(".layer_bg").css("display", "none");
            });


        });


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <div style=" margin-top:20px;">
         <table align="center" border="1" bordercolor="#b7dcef" cellpadding="1" cellspacing="1"
                    width="99%">
                   
                    <tr>
                        <td align="right" width="25%" height="30px" class="auto-style1">
                            药品生产批件有效期:</td>
                        <td align="left" width="30%" class="auto-style1">
                            
                        <asp:TextBox ID="tendtime" runat="server" onClick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd'})" Width="123px"></asp:TextBox>
                        <asp:Label ID="Label2" runat="server" Text="*" ForeColor="#CC0000"></asp:Label>
                        </td>
                      
                   <td>

                       点击图片查看大图
                   </td>
                      
                    
                    </tr>
                 
              
                    
                    
                </table>
       </div>
        <div style="width: 100%;">
            <div id="upBox">
                <asp:HiddenField ID="HiddenField1" runat="server" />
                 <asp:HiddenField ID="HiddenField2" runat="server" />
                 <div id="inputBox">

//点击FileUpload1上传浏览按钮,通过javascript:__doPostBack('lbUploadPhoto',''),直接执行lbUploadPhoto按钮的方法
                     <asp:FileUpload ID="FileUpload1" runat="server"  οnchange="javascript:__doPostBack('lbUploadPhoto','')"  />
                      <asp:LinkButton ID="lbUploadPhoto" runat="server" OnClick="lbUploadPhoto_Click" CssClass="yincang"></asp:LinkButton> 
                     点击选择药品生产批件


                 </div>
                 <div id="imgBox">

                        <asp:DataList ID="CFlist" runat="server" BackColor="White" 
                BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList1_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
                <AlternatingItemStyle BackColor="#F7F7F7" />
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" BorderColor="black" />
            
                <ItemTemplate >
   <table style="border-color:Black">
                        <tr>
                            <td  style="border-style:none">
                            <img alt="<%#Eval("imagename") %>" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>

//onclick方法里是点击图片看大图的js
                            </td>
                            </tr>
                           <tr>
                            <td style="height:40px;width:100px;border-style:none" >
                                <center> <asp:LinkButton ID="lbDe" CommandArgument=<%#Eval("id") %> CommandName="De" runat="server">删除</asp:LinkButton></center></td>
                        </tr>
                   
                    
                       
                     </table>   
                   
                </ItemTemplate>
                <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            </asp:DataList>


                    
                 </div>
                 


                
            </div>

                <div id="upBox2">


                     <div id="inputBox2">
                       <asp:FileUpload ID="FileUpload2" runat="server"  οnchange="javascript:__doPostBack('lbUploadPhoto2','')"  />
                      <asp:LinkButton ID="lbUploadPhoto2" runat="server"  CssClass="yincang" OnClick="lbUploadPhoto2_Click"></asp:LinkButton> 
                    
                         点击选择药品包装及说明书</div>
                 <div id="imgBox2">

                     <asp:DataList ID="DataList2" runat="server" BackColor="White" 
                BorderColor="#E7E7FF" CellPadding="3" 
                GridLines="Horizontal"  Width="99%" onitemcommand="DataList2_ItemCommand" RepeatDirection="Horizontal" RepeatColumns="5">
                <AlternatingItemStyle BackColor="#F7F7F7" />
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" BorderColor="black" />
            
                <ItemTemplate >
   <table style="border-color:Black">
                        <tr>
                            <td  style="border-style:none">
                            <img alt="<%#Eval("imagename") %>" src='<%#Eval("imageurl") %>' style="height:270px ;cursor:hand" οnclick=" $('.layer_con img').prop('src', this.src);layer($(this));"/>
                            </td>
                            </tr>
                           <tr>
                            <td style="height:40px;width:100px;border-style:none" >
                                <center> <asp:LinkButton ID="lbDe" CommandArgument=<%#Eval("id") %> CommandName="De" runat

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 jQuery插件jCrop来对图片进行预览和裁剪,然后使用 ASP.NET MVC的文件上传功能将裁剪后的图片上传到服务器。 以下是大致的实现步骤: 1. 在视图中引入 jCrop 插件和 jQuery 库: ``` <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/css/jquery.Jcrop.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jcrop/0.9.15/js/jquery.Jcrop.min.js"></script> ``` 2. 在视图中显示待裁剪的图片: ``` <img src="path/to/image" id="crop-image" /> ``` 3. 使用 jCrop 插件初始化图片裁剪: ``` <script> $(function () { $('#crop-image').Jcrop({ aspectRatio: 1, onSelect: updateCoords }); }); function updateCoords(c) { $('#x').val(c.x); $('#y').val(c.y); $('#w').val(c.w); $('#h').val(c.h); }; </script> ``` 4. 添加表单元素用于提交裁剪的坐标: ``` <form method="post" action="Upload" enctype="multipart/form-data"> <input type="hidden" name="x" id="x" /> <input type="hidden" name="y" id="y" /> <input type="hidden" name="w" id="w" /> <input type="hidden" name="h" id="h" /> <input type="file" name="file" /> <input type="submit" value="Upload" /> </form> ``` 5. 在控制器中实现文件上传图片裁剪: ``` [HttpPost] public ActionResult Upload(HttpPostedFileBase file, int x, int y, int w, int h) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var filePath = Path.Combine(Server.MapPath("~/Uploads"), fileName); var img = Image.FromStream(file.InputStream); // 裁剪图片 var croppedImg = new Bitmap(w, h); var g = Graphics.FromImage(croppedImg); g.DrawImage(img, new Rectangle(0, 0, croppedImg.Width, croppedImg.Height), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); croppedImg.Save(filePath); return RedirectToAction("Index"); } return View(); } ``` 这样就可以在 ASP.NET MVC 中实现图片预览、裁剪和上传了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值