asp.net 上传图片(包括预览图,js)

15 篇文章 0 订阅
  //   js实现预览功能
< script  type ="text/javascript">
     function  readURL(input) {
         if  (input.files && input.files[0]) {
             var  reader =  new  FileReader();
            reader.onload =  function (e) { $( '#img_prev'  ).attr( 'src' , e.target.result).width(544).height(168); };
            reader.readAsDataURL(input.files[0]);
        }  else  {
             //IE 下,使用滤镜
             var  docObj = document.getElementById( 'inputFile' ); docObj.select();
             var  imgSrc = document.selection.createRange().text;
             var  localImagId = document.getElementById( "localImag" );
            $(  '#localImag' ).width(544).height(168);  // 必须设置初始大小
             // 图片异常的捕捉,防止用户修改后缀来伪造图片
             try  {
                localImagId.style.filter =  "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"  ;
                localImagId.filters.item(  "DXImageTransform.Microsoft.AlphaImageLoader"  ).src = imgSrc;
            }  catch  (e) {
                alert(  " 您上传的图片格式不正确,请重新选择  !" );  return  false ;
            }
            $(  '#img_prev' ).hide();
            document.selection.empty();
        }
    }   
</ script >


< body >
     < form  id ="form1"  runat ="server">
     < div ><  span >< strong  > 请首先指定 Banner 图片  : </ strong  ></ span >
         < asp :  DropDownList  ID  ="DropDownList1"  runat ="server"
             onselectedindexchanged ="DropDownList1_SelectedIndexChanged"  AutoPostBack  ="true">
             < asp :  ListItem  Selected  ="True"  Value  ="0"> 新闻一 </  asp : ListItem  >
             < asp :  ListItem  Value  ="1"> 新闻二 </  asp : ListItem  >
             < asp :  ListItem  Value  ="2"> 新闻三 </  asp : ListItem  >
             < asp :  ListItem  Value  ="3"> 新闻四 </  asp : ListItem  >
             < asp :  ListItem  Value  ="4"> 广告一 </  asp : ListItem  >
             < asp :  ListItem  Value  ="5"> 广告二 </  asp : ListItem  >
             < asp :  ListItem  Value  ="6"> 广告三 </  asp : ListItem  >
             < asp :  ListItem  Value  ="7"> 广告四 </  asp : ListItem  >     
         </ asp :  DropDownList >< br  />< br  />
         < table  width ="65%"  border ="0"  id ="tab"  cellpadding ="0"  cellspacing ="1"  bgcolor ="#a8c7ce">
             < tr >
                 < td  width ="20%"  height ="30"  bgcolor ="#e1e5ee"  style =" text-align  : left">
                     < div  align ="center">
                         < span  class ="STYLE10"> 请选择图片:  < br  /> ( 图片大小  680*210) </ span  >
                     </ div >
                 </ td >
                 < td  width ="80%"  height ="20"  bgcolor ="#e1e5ee"  style =" text-align  : left">
                     < input  id ="inputFile"  style =" width  :  584px"  type ="file"    onchange ="readURL(this);"  runat  ="server"/>
<%  --                    <input type='file' id="doc" οnchange="readURL(this);" /> --  %>
                 </ td >
             </ tr >
             < tr >
                 < td  width ="20%"  height ="20"  bgcolor ="#e1e5ee"  style =" text-align  : left">
                      < div  align ="center">
                       < span  class ="STYLE10"> 预览图:  </ span ></  div >
                 </ td >
                 < td  width ="80%"  bgcolor ="#e1e5ee"  style =" text-align  : left"  height ="20px">
                     < div  id ="localImag">< img  id ="img_prev"  name =""  src =""  alt ="your image"    Height ="168px"  Width ="544px"  /></ div >
                    < div  style =" display  : none">  < asp :  TextBox  ID  ="imgText"  Text  =""  Visible  ="true"  runat ="server"></ asp :  TextBox ></ div  >
    <% --                <asp:Image ID="Image1" runat="server"  Height="168px" Width="544px" />-- %>
                 </ td >
             </ tr >
             < script  type ="text/javascript">
                $(document).ready(  function () {
                    $(  '#img_prev' ).attr( "src"  , $( '#imgText' ).val());
                    $(  "#DropDownList1"  ).change( function () {
                        $(  '#img_prev' ).attr( "src"  , $( '#imgText' ).val());
                    });
                     if  ($( "#DropDownList1"  ).val() > 3) {
                        $(  "#introTr" ).css( "display"  ,  "" );
                    }
                     else  {
                        $(  "#introTr" ).css( "display"  ,  "none" );
                    }
                });
             </ script >
            < tr  id ="introTr"  style =" display  : ">
                < td  width ="20%"  height ="20"  bgcolor ="#e1e5ee"  style =" text-align  : left">
                      < div  align ="center">
                                 < span  class ="STYLE10"> 请输入文字:  </ span ></  div >
                 </ td >
                 < td  width ="80%"  height ="20"  bgcolor ="#e1e5ee"  style =" text-align  : left">
                     < asp :  TextBox  ID  ="TextBox1"  runat  ="server"  Height  ="140px"  TextMode ="MultiLine"
                         Width ="443px"></ asp :  TextBox >
                 </ td >
             </ tr >

             < tr  >
                 < td  height ="50"  bgcolor ="#e1e5ee"  style =" text-align  : center">


                 </ td >
                 < td  height ="50"  bgcolor ="#e1e5ee"  style =" text-align  : center">
                   < asp :  Button  ID  ="btnSave"  runat  ="server"  Text  =" 上传 "  onclick ="btnSave_Click"  />
                   &nbsp;&nbsp;&nbsp;
                   < asp :  Button  ID  ="btnDelete"  runat  ="server"  Text  =" 删除 "  onclick ="btnDelete_Click"  />
                 </ td >
             </ tr >
         </ table >
     </ div >
     </ form >
</ body >


后台:

using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;
using  BizComponents;
using  Entity;
using  System.Collections.Generic;
using  System.Text.RegularExpressions;
using  System.Text;
namespace  Admin.Banner
{
     public  partial  class  BannerSet  : System.Web.UI. Page
    {
         public  int  total;
         public  int  sentTotal;
         Banner_Biz  biz =  new  Banner_Biz ();

         protected  void  Page_Load( object  sender,  EventArgs  e)
        {
             if  (!IsPostBack)
            {
                 Banner  info = getInfo();
                TextBox1.Text = info.Introduction;
                imgText.Text =  "/images/"  + info.Picture;
            }

        }

         protected  void  btnSave_Click( object  sender,  EventArgs  e)
        {
             try
            {
                 string  imgName = inputFile.Value;
                 if  (! string  .IsNullOrEmpty(imgName) || ! string .IsNullOrEmpty(imgText.Text.Replace( "/images/" ,  "" )))
                {
                     string  name = imgName; //  获取文件名   
                     string  type = name.Substring(name.LastIndexOf( "." ) + 1);       // 获取文件类型
                     string  fileName =  DateTime  .Now.ToString( "yyyyMMddHHmmss" );
                     string  ipath = Server.MapPath( "../../images"  ) +  "\\"  + fileName +  "."  + type;       //  获取文件路径   
                     if  (type ==  "jpg"  || type ==  "gif"  || type ==  "bmp"  || type ==  "png"  || type ==  "jpeg"  || type ==  "" )
                    {
                         if  (inputFile.PostedFile.ContentLength <= 1024000)
                        {
                             Banner  info =  new  Banner ();
                            info.Picture = fileName +  "."  + type;
                            info.Introduction = TextBox1.Text;
                             if  ( Convert  .ToInt32(DropDownList1.SelectedValue) >= 4)
                            {
                                info.IsNews =  false ;
                                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) - 3;
                            }
                             else
                            {
                                info.IsNews =  true ;
                                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) + 1;
                            }

                             string  transfer =  "BannerSet.aspx?_"  +  DateTime .UtcNow.Ticks;
                             try
                            {
                                 int  result = 0;
                                 Banner  info1 = getInfo();
                                 if  ( string  .IsNullOrEmpty(imgName))
                                {
                                    info.Picture = info1.Picture;
                                }
                                 if  (info1 !=  null  && info1.ID != 0)
                                {
                                    info.ID = info1.ID;
                                    info.Status = 1;
                                    info.AddTime = info1.AddTime;
                                    result = biz.Banner_Update(info);
                                }
                                 else
                                {
                                    result = biz.Banner_ADD(info);
                                }
                                 if  (result > 0)
                                {
                                     if  (! string  .IsNullOrEmpty(imgName))
                                    {
                                        inputFile.PostedFile.SaveAs(ipath);
                                    }
                                    Response.Write(  "<script>alert('  图片上传成功!  ');location.href='"  + transfer +  "'</script>"  );
                                }
                                 else
                                {
                                    Response.Write(  "<script>alert('  图片上传失败!  ')'</script>" );
                                }
                            }
                             catch  ( Exception  ex)
                            {
                                 Logger .Error( "  图片上传出错  " , ex);
                                Response.Write(  "<script>alert('"  + ex.Message +  "')</script>" );
                            }
                        }
                         else
                        {
                            ClientScript.RegisterStartupScript(  this .GetType(),  ""  ,  "alert(' 图片不能超时  1M')" ,  true  );
                        }
                    }
                     else
                    {
                        ClientScript.RegisterStartupScript(  this .GetType(),  ""  ,  "alert(' 图片格式不正确  ')" ,  true  );
                    }
                }
                 else
                {
                    ClientScript.RegisterStartupScript(  this .GetType(),  ""  ,  "alert(' 请选择上传图片  ')" ,  true  );
                }

            }
             catch  ( Exception  ex)
            {
                 Logger .Error( "  上传图片失败  " , ex);
            }
        }

         protected  void  btnDelete_Click( object  sender,  EventArgs  e)
        {
             string  transfer =  "BannerSet.aspx?_"  +  DateTime .UtcNow.Ticks;
             Banner  info =  new  Banner ();
             if  ( Convert  .ToInt32(DropDownList1.SelectedValue) >= 4)
            {
                info.IsNews =  false ;
                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) - 3;
            }
             else
            {
                info.IsNews =  true ;
                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) + 1;
            }
            info = biz.Banner_GetAll(info.Sort, info.IsNews);
             if  (info !=  null  && info.ID != 0)
            {
                 try
                {
                     int  result = biz.Banner_DelByID(info.ID); // 调用删除方法
                     if  (result > 0)
                    {
                        Response.Write(  "<script>alert('  删除图片成功!  ');location.href='"  + transfer +  "'</script>"  );
                    }
                     else
                    {
                        Response.Write(  "<script>alert('  删除图片失败!  ')'</script>" );
                    }
                }
                 catch  ( Exception  ex)
                {
                     Logger .Error( "  删除图片出错  " , ex);
                    Response.Write(  "<script>alert('"  + ex.Message +  "')</script>" );
                }
            }
        }

         protected  void  DropDownList1_SelectedIndexChanged(  object  sender,  EventArgs  e)
        {
             Banner  info = getInfo();
            TextBox1.Text = info.Introduction;
            imgText.Text =  "/images/"  + info.Picture;
        }

         public  Banner  getInfo()
        {
             Banner  info =  new  Banner ();
             if  ( Convert  .ToInt32(DropDownList1.SelectedValue) >= 4)
            {
                info.IsNews =  false ;
                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) - 3;
                TextBox1.Visible =  true ;
            }
             else
            {
                info.IsNews =  true ;
                info.Sort =  Convert .ToInt32(DropDownList1.SelectedValue) + 1;
                TextBox1.Visible =  false ;
            }
            info = biz.Banner_GetAll(info.Sort, info.IsNews);
             return  info;
        }
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值