FileUpload上传图片直接浏览显示(没有上传按钮如何上传)

1、给FileUpload添加一个onchange事件:FileUpload1.Attributes.Add("onchange", "document.getElementById('imgP').src=this.value");

2、原来是单独的上传事件,现在没有上传按钮了,将上传的事件(代码)直接放入要执行的插入(提交信息)按钮事件中。

protected void btnSubmit_Click(object sender, EventArgs e)
{
      if (IsValid)

      {

      //先执行上传事件

            string picName = "";
            string newpicName = "";
            string file = "/images/small/";
            string newPath = "/images/small/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "-" + DateTime.Now.Day + "/";
            UploadPicture.CreatDirectory(newPath);

                ...........................

   

               //此时也可以添加做好的水印

            picName = UploadInfo(fulImage, file);
            newpicName = UploadInfo(fulImage, newPath);

            System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(picName));
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath("/images/shuiyin.jpg"));
            Graphics g = Graphics.FromImage(image);
            g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0,                      copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g.Dispose();
            //保存加水印过后的图片,删除原始图片
            image.Save(Server.MapPath(newpicName));
            image.Dispose();
            if (File.Exists(Server.MapPath(picName)))
            File.Delete(Server.MapPath(picName));

     }
{

原文地址:https://www.cnblogs.com/candyzhmm/p/4713587.html