我想在在图片上传到服务器之前 ,就预览图片。而我现在做的是,把图片先上传到服务器后,在进行预览,只不过此时还没把图片路径插入数据库,,,,,这样的话,如果有谁不停的预览。那么图片就都上传到我服务器了。我想不上传到服务器就预览图片,求解,,,,下面是我 点击 预览的事件代码:  //预览图片
        protected void btn_YuLan_Click(object sender, EventArgs e)
        {
            type = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取图片的格式
            filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + type;
            if (type == "gif" || type == "jpg" || type == "bmp" || type == "GIF" || type == "JPG" || type == "BMP")
            {                this.FileUpload1.PostedFile.SaveAs(Server.MapPath("../Upload/UserPic/") + "\\" + filename);//放在web服务器上的路径
                path = "../Upload/UserPic/" + filename;
                this.Image1.ImageUrl = path;
                this.Image1.ImageUrl = System.Drawing.Image.FromStream(postFile.InputStream, true);
                size = (FileUpload1.PostedFile.ContentLength / 1024).ToString();                Label1.Text = type;
                Label2.Text = size;
            }
            else
            {
                Response.Write("<script>alert('格式不正确请上传:gif,jpg,bmp格式图片')</script>");
            }
        }此时没没把数据插入数据库,但已上传到服务器了

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class UploadPicture : System.Web.UI.Page
    {
        // 画面装载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["ID"] != null)
                {
                    btnUpload.Enabled = true;
                    ViewState["ID"] = Request.Params["ID"].ToString();
                }
                else
                {
                    btnUpload.Enabled = false;
                    ViewState["ID"] = "";
                }
            }
        }
        // 上传图片
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (tbxPath.Value.ToString() == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "请指定上传文件路径!";
            }
            else if (tbxPath.PostedFile.ContentLength == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "文件的内容不能为空!";
            }        // 获取上传图片的文件名
            String fileName = tbxPath.PostedFile.FileName.Substring(tbxPath.PostedFile.FileName.LastIndexOf("\\"));        if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName))
            {
                lblMessage.Visible = true;
                lblMessage.Text = "该文件名在服务器中已存在,请更改文件名!";
            }
            else
            {
                try
                {
                    // 上传文件
                    tbxPath.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName);
                    // 添加数据库信息
                    DBUtil.DBUtil_Picture.AddItem(tbxName.Text.Trim(),
                                                  Request.ApplicationPath + "\\Pictures" + fileName,
                                                  "False",
                                                  ViewState["ID"].ToString());
                    // 显示上载成功的信息
                    lblMessage.Visible = true;
                    lblMessage.Text = "文件上传成功!";
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "由于网络原因,上载文件错误  " + ex.Message;
                }            tbxName.Text = "";
            }
        }
        // 返回图片列表
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/PictureList.aspx");
        }
    }试试可以!
      

  2.   

    图片上传服务器的这段代码别写在预览里  这样没次预览都会上传一次。。每次图片的名称都会变,这样会出现bug (我以前遇到过)你可以将图片上传到临时的一个地方预览的路径指向这个临时的地方 当你真正上传和查数据库的时候再一次性完成