简体   繁体   中英

How to display image after i select from FileUpload without upload button

I want that when i select image via FileUpload1 the image should be displayed without any upload button.My code is below:

  protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string str = FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//Uploads//" + str);
                string menuImage = "~//Uploads//" + str.ToString();
             //   Image1.ImageUrl = menuimage.ToString();
                string encryptedpassword = Encryptdata(txtpassword.Text);
                string encryptedconfirmpassword = Encryptdata(txtconfirmpassword.Text);
                try
                {
                    Databasebase.NewEmployee(txtname.Text, menuImage.ToString(),

as I understood,you want to preview image before saving,so :

html :

<div>
  <asp:FileUpload ID="fulImg" runat="server" />
  <asp:Button ID="btnUploadImg" runat="server" Text="Upload" OnClick="btnUploadImg_Click" />
  <asp:Image ID="img" runat="server" />
</div>

and here is code behind :

protected void btnUploadImg_Click(object sender, EventArgs e)
    {
        Session["Image"] = fulImg.PostedFile;
        Stream fs = fulImg.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        byte[] bytes = br.ReadBytes((Int32)fs.Length);
        string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
        img.ImageUrl = "data:image/png;base64," + base64String;
    }

and finally for saving image :

HttpPostedFile postedFile = (HttpPostedFile)Session["Image"];
string imageName = Guid.NewGuid().ToString() + Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(Server.MapPath("yourPath") + imageName);

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM