Click here to Skip to main content
15,031,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Please, Help how can I read stream from the PostedFile in Gridview However HasFile property is true in GridView ASP:FileUpload control. FileUpload has been found through this

FileUpload FileUpload1 = (FileUpload)row.Cells[0].FindControl("FileUpload1");

and Used this snippet to save the data in Database but byte[] pic has all zero (0) values in it after execute FileUpload1.PostedFile.InputStream.Read(pic, 0, len); this statement.

      int len = FileUpload1.PostedFile.ContentLength;
      byte[] pic = new byte[len];
      FileUpload1.PostedFile.InputStream.Read(pic, 0, len);      

what would be the solution?.... to load data in byte[] pic however this snippet is working fine out side the Gridview.
imaa2amha 12-Oct-11 9:32am
try to use

Request.Files if it will give u the files

Try this,

if (FileUpload1.HasFile)
    string contentType = FileUpload1.PostedFile.ContentType;

    // Get the bytes from the uploaded file
    byte[] fileData = new byte[FileUpload1.PostedFile.InputStream.Length];
    FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length);
My Solution.....

I have to work by Streaming...
first of all add..

in the gridview on RowCommand

Upload Document...

FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);

// Allocate your data to DB nVarBinary field.
oelScanImages.ImageFile = bytes;


Thanks to all who participate to assist me.
Try this...

Byte[] imgByte = null;
   if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
      //To create a PostedFile
      HttpPostedFile File = FileUpload1.PostedFile;
      //Create byte Array with file len
      imgByte = new Byte[File.ContentLength];
      //force the control to load data in array
      File.InputStream.Read(imgByte, 0, File.ContentLength);
      originalSize = File.ContentLength;


Please vote as answer if help...


fareedulhassan 13-Oct-11 1:08am
This code is not working in GridView as I have told before the function File.InputStream.Read(imgByte, 0, File.ContentLength) not buffering in imgByte array it keeps imgByte all zero in array however here should be some hexa value etc.

Question is that How can we buffer data in imgByte using this File.InputStream.Read(,,,)

Thanks for your striving but not a solution.
Al Moje 13-Oct-11 1:23am
I am sorry for not hitting your question. Maybe my answer could fit to you second question ‘How can we buffer data in imgByte’. Yes that's the way…

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