点击Button按钮直接弹出文件选择对话框,选择图片之后在图片控件显示,并将该图片的二进制存起来,传到另一个页面
要求:
1.在.net环境下(网页)
2.点击按钮直接弹出文件选择对话框(这个已完成,附代码)
3.选择图片后直接显示(img控件),并不保存;
4.把图片的二进制保存(session最好),需要跨页面传值,到另一个页面点保存时才存进数据库<img src="../Code_Company/Image.aspx" onerror="this.src='../Image/white.jpg'" width="178"
            height="167" style="border: solid 1px #232323" id="img" runat="server" /><br />
        <asp:FileUpload ID="ImageUpload"  Style="display: none;" runat="server" />
        <asp:Button ID="btnUpImg" runat="server" Text="浏览" CssClass="button" Style="margin-top: 4px;"
            OnClientClick="ImageUpload.click();" OnClick="btnUpImg_Click" />说说第3点遇到我问题
  浏览按钮(btnUpImg),做了OnClientClick事件之后,就没做OnClick事件!
因为要OnClientClick返回true才做OnClick事件 
我这的事件根本没有返回(OnClientClick="ImageUpload.click();" );
其实第3点用js是可以实现的。
但我要取到图片的二进制并跨页面传 这个js做不到
先说说我的思路
1.浏览按钮做完OnClientClick事件之后,不是弹出了文件选择对话框去,在文件选择对话框点击保存时做事(微软的控件,取不到它的事件)
2.用js做第三点,然后在js中想办法取得图片的二进制,并跨页面传值
请高手指点
ps1:需求不更改,改了谁都会!
ps2:说了够详细了吧,在线等
ps3:晕,只能给100,谁帮我解决了,我另开贴给分

解决方案 »

  1.   

    style type="text/css">#newPreview {}{ 
    FILTER: progidXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale) 

    </style> 
    <body>内的代码 
    <script language="javascript" type="text/javascript"> 
    function PreviewImg(imgFile) 
    {  
        var newPreview = document.getElementById("newPreview"); 
        newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value; 
        newPreview.style.width = "80px"; 
        newPreview.style.height = "60px";
        //通过ajax或document.getElementByID("").Click();_doPostBack('','');执行后台事件获取二进制通过web services传递 

    </script> <asp:FileUpload ID="Fud_Pic" runat="server" onchange="PreviewImg(this)"/> 
    <div id="newPreview"> </div> 
      

  2.   


    view plaincopy to clipboardprint?
    #region 上传图片到数据库   
      private void UpIMGButton_Click(object sender, System.EventArgs e)   
      {     
       string exName=UpFile.Value.Substring(UpFile.Value.LastIndexOf(".")+1).ToUpper();//找出图片的后缀名   
          string ImgName=DateTime.Now.ToString("yyyyMMddhhmmssfff")+"."+exName;   
       if (UpFile.PostedFile.ContentLength==0)   
       {    
        Response.Write("<script> alert('你上传的图片不能为空!');</script>");   
       }   
       else  
       {    
        try  
        {   
         Byte[] FileByte = new byte[UpFile.PostedFile.ContentLength];   
         Stream  ObjectStream = UpFile.PostedFile.InputStream;   
         ObjectStream.BeginRead(FileByte,0,UpFile.PostedFile.ContentLength,null,null);   
         string imgType=UpFile.PostedFile.ContentType;   
                        Byte[] SmallFileByte = new byte[UpFile.PostedFile.ContentLength];   
                         SmallFileByte=CreateThumnail(ObjectStream,100,100);   
      
        string ConStr ="UID=sa,PWD=sa,Server=local,Database=mydb";   
       SqlConnection Conn = new SqlConnection(ConStr);   
        Conn.Open();   
            SqlCommand  myCommand =new SqlCommand();   
         myCommand.Connection=Conn;   
            myCommand.CommandText="insert into [UpImage] (imageName,image,imgType,SmallImage) values (@ImgName,@FileByte,@imgType,@SmallImage)";   
         myCommand.Parameters.Add("@ImgName",ImgName);   
         myCommand.Parameters.Add("@FileByte",FileByte);   
         myCommand.Parameters.Add("@imgType",imgType);   
         myCommand.Parameters.Add("@SmallImage",SmallFileByte);   
         myCommand.ExecuteNonQuery();   
         Response.Write("<script> alert('图片保存到数据库成功!');</script>");   
        }   
        catch(Exception ex)   
        {   
         Response.Write (ex.Message);   
           }   
           
       }   
      }   
      #endregion   
      #region 生成缩略图   
      private Byte[]  CreateThumnail(Stream ImageStream,int tWidth, int tHeight)   
        {   
       System.Drawing.Image g  =  System.Drawing.Image.FromStream(ImageStream);   
       int[] thumbSize = new int[]{1,1};   
       thumbSize = NewthumbSize(g.Width, g.Height, tWidth, tHeight);   
       Bitmap imgOutput = new Bitmap(g, thumbSize[0], thumbSize[0]);   
       MemoryStream imgStream = new MemoryStream();   
       System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;   
       imgOutput.Save(imgStream, thisFormat);   
       Byte[] imgbin =new byte[imgStream.Length];   
       imgStream.Position = 0;   
       Int32 n = imgStream.Read(imgbin,0,imgbin.Length);   
       g.Dispose();   
       imgOutput.Dispose();   
       return imgbin;   
      }   
      #endregion   
      
      #region 根据上传图片调整缩略图的尺寸   
          
      protected int[] NewthumbSize(int currentwidth,int currentheight,int newWidth ,int newHeight)   
      {   
       int tempMultiplier;   
       if(currentheight > currentwidth)   
       {    
        tempMultiplier = newHeight / currentheight;   
       }   
          else  
       {   
         tempMultiplier = newWidth / currentwidth;   
       }   
      
       int[] NewSize = new int[]{(currentwidth * tempMultiplier),(currentheight * tempMultiplier)};   
         return NewSize;   
      }   
      #endregion   
      
    ////图片显示页的代码   
     myCommand.CommandText="select smallimage from [UpImage] ";   
       SqlDataReader dr =db.myCommand.ExecuteReader();   
       this.Response.ContentType="image/gif";   
       while(dr.Read())   
       {   
        Response.BinaryWrite((byte[])dr["smallimage"]);   
          }