zoukankan      html  css  js  c++  java
  • 文件上传

    主要用到的是FileUpload控件

    属性:
    FileName:文件名
    HasFile:bool 是否选中了文件
    FileBytes:要上传文件的二进制数据
    方法:
    SaveAs(string 绝对路径):上传,另存为。

    MapPath——把相对路径转化为绝对路径

    上传文件分两种形式:

    一,上传到硬盘文件夹

    1,传单个文件

     protected void btnUpload_Click(object sender, EventArgs e)
        {
           
            //把之前在客户端的文件名给取出来
            string fileName = FileUpload1.FileName;
    
            //防止文件重名
            fileName = DateTime.Now.ToString("yyyyMMddhhmmsss") + fileName;
    
            //把相对路径转化为绝对路径
            string path = Server.MapPath("uploads/" + fileName);
    
            //上传
            FileUpload1.SaveAs(path);   //参数必须根路径
    
        }

    2.传多个文件

    思路:遍历form表单中的所有FileUPload控件,如果选中了文件则上传

     protected void Button1_Click(object sender, EventArgs e)
        {
            int index = 0;
            foreach (Control ctrl in form1.Controls)
            {
                if (ctrl is FileUpload)
                {
                    index++;
                    //取得每个上传控件
                    FileUpload upload = ctrl as FileUpload;
                    //上传控件中选上文件了
                    if (upload.HasFile)
                    {
                        //做文件路径出来
                        string path = Server.MapPath("uploads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + index.ToString("00") + upload.FileName);
    
                        //上传
                        upload.SaveAs(path);
                    }
                }
            }
        }

    二,上传到数据库Image列

    (一)传到数据库中去

    1.在数据库中建表(注意Image列的类型为image类型)

    2.编写按钮点击的代码

    protected void Button1_Click(object sender, EventArgs e)
        {
            //取界面数据
            PhotoData data = new PhotoData();
            data.Name = FileUpload1.FileName;
            data.Pic = FileUpload1.FileBytes;  //FileBytes保存的是图片的二进制数据
            //送到数据库去
            new PhotoDA().Insert(data);
        }

    (二)从数据库中找出来并显示出来

    思路:单独做一个用来显示图片二进制数据的页面。把这个页面赋给Image控件。

    protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request["id"];
            PhotoData pic = new PhotoDA().Select(Convert.ToInt32(id));
            if (pic!=null)
            {
                byte[] bf = pic.Pic;  //定义一个二进制数组用来接收图片二进制数据
                Response.OutputStream.Write(bf, 0, bf.Length);//用response输出流直接打到客户端显示
                Response.End();
            }
        }

    再在原页面的按钮点击代码中对image控件的URL属性赋值

     protected void Button3_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "ShowPIC.aspx?id=" + TextBox1.Text;
        }

    ********************************************************************************************************************

    上传图片加水印效果制作

    1.需要引用命名空间

    using System.Drawing;
    using System.IO;

    2.将上传的数据转化为图片对象

    3.对图片对象进行画水印

    4.图片对象另存到硬盘上去

    protected void Button1_Click(object sender, EventArgs e)
        {
            //一、从上传数据中,转化成图片对象。
            Stream s = FileUpload1.FileContent;
            System.Drawing.Image img = System.Drawing.Image.FromStream(s);  //直接写image会产生二义性,故前面要加上命名空间
    
            //二、对图片对象进行画水印
            //1.造笔
            SolidBrush brush = new SolidBrush(Color.Yellow);
            //2.造字体
            Font font = new Font("Comic Sans MS", 18);
            //3.找到图像绘图区域
            Graphics g = Graphics.FromImage(img);
            //4.确定开始画位置
            SizeF size = g.MeasureString("http://www.itNBA.com",font);  //测量这段文字的尺寸
            float x = 0, y = 0;
            y = img.Height - size.Height;
            x = img.Width - size.Width;
            //5.开始画
            g.DrawString("http://www.itNBA.com", font, brush, x, y);
    
            //三、图片对象另存到硬盘上去
            string fileName = FileUpload1.FileName;
            string path = Server.MapPath("uploads/" + fileName);
            img.Save(path);
        }

    ***************************************************************************************************************************

    给表单加一个图片验证码

    1.造一个生成验证码的页面

    2.在表单页面添加一个image控件,将其imageUrl属性设置成该页面

     protected void Page_Load(object sender, EventArgs e)
        {
            //造一个图片
            Bitmap img = new Bitmap(60, 30);
            //生成个随机数
            string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            Random rand = new Random();
            string code = "";
            for (int i = 0; i < 4; i++)
            {
                int start = rand.Next(str.Length);
                code += str.Substring(start, 1);
            }
            Session["yzm"] = code;
            //把随机数画到图片上
            SolidBrush brush = new SolidBrush(Color.White);
            Font font = new Font("Lucida Sans Unicode", 14);
            Graphics g = Graphics.FromImage(img);
    
            //a.把图片背景涂白
            g.FillRectangle(brush, 0, 0, 60, 30);
            //b.给画笔换个颜色
            brush.Color = Color.Red;
    
    
            g.DrawString(code, font, brush, 0, 0);
    
            //把图片一jpg的格式保存到输出流中去
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    
            Response.End();
        }

  • 相关阅读:
    IE8下部分方法失效的解决方法
    C#获取本机IP地址(ipv4)
    WPF中控制窗口显示位置的三种方式
    JS判断IP的正则表达式
    WPF ListView 简单的拖拽实现(转)
    C# 中取绝对值的函数
    移动端rem单位适配使用
    vue中遇到的坑!!!!!
    vux安装中遇到的坑
    移动端常用的代码
  • 原文地址:https://www.cnblogs.com/William-1234/p/4541922.html
Copyright © 2011-2022 走看看