1

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(),
4

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.