Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
This code works perfectly well
C#
Session["fileUpl"] = fileUpl; lblFileUpload.Text = fileUpl.FileName;
            Session["Image"] = fileUpl.PostedFile;
            Stream fs = fileUpl.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;


but this code below does not works ,fs.length shows file lenght but bites array shows 0 bytes .that means binary reader is not happy with something .i dont know why please help.

C#
fileUpl = (FileUpload)Session["fileUpl"]; lblFileUpload.Text = fileUpl.FileName;
           HttpPostedFile postedFile = (HttpPostedFile)Session["Image"];
           Stream fs = postedFile.InputStream;
           BinaryReader br = new BinaryReader(fs);
           byte[] bytes = br.ReadBytes((Int32)fs.Length);
           string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
           Image1.ImageUrl = "data:image/png;base64," + base64String;
Posted

1 solution

put byte[] of PostedFile to the session and access it next time
C#
Stream fs = fileUpl.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Session["bytes"] = br.ReadBytes((Int32)fs.Length);

then
C#
byte[] bytes = (byte[])Session["bytes"]
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
 
Share this answer
 
Comments
varun150 11-Apr-15 1:45am    
i put the base64string in session and it worked,Lol silly me,Thankyou so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900