Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Ajax
Hello experts, i recently tried to insert images via the AsyncFileupload control in asp.net. The Thing is everything works fine but all the Images(datatype Image) in the database are stored as 0x000000000000000...which means they are all null bytes right?? that there is no readable data there right??
I wrote the same code for Fileupload control of asp.net and it works well and the image bytes are clearly 0xfe543werrrr.....smth...I don't really know what the problem is....here are my codes
 
protected void btnSubmit_Click(object sender, EventArgs e)
    {
 
        byte[] bytImageBytes = null;
        System.IO.Stream myStream;
        Int32 nFileLength;
        if (AsyncFileUpload1.PostedFile != null && AsyncFileUpload1.PostedFile.FileName != null)
        {
            nFileLength = AsyncFileUpload1.PostedFile.ContentLength;
            bytImageBytes = new byte[nFileLength];
 
            myStream = AsyncFileUpload1.PostedFile.InputStream;
            myStream.Read(bytImageBytes, 0, nFileLength);
            
 

 
            if (dbOps.InsertBodInformation(txtFullName.Text, txtDesig.Text, txtShortDesc.Text,
                txtLongDesc.Text, bytImageBytes))
            {
                Response.Write(@"<script language="'javascript'">
                           alert('Insertion Successful!')</script>");
            }
            else
            {
                Response.Write(@"<script language="'javascript'">
                           alert('Error Writing to the database!')</script>");
            }
 
        }
}
 
The InsertBodInformation function returns bool type...and returns true.
Please help me out.
Thanks in Advance
Mlimbu
Posted 13-Oct-12 6:09am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This may work for you.
 
Instead of trying to read the whole file all at once, read it in chunks.
myStream = AsyncFileUpload1.PostedFile.InputStream;
int bufferSize = 1024;
long bytesRead = 0;
int curPos = 0;
 
while (bytesRead < nFileLength)
{
    bytesRead += myStream.Read(bytImageBytes, curPos, bufferSize);
    curPos += bufferSize;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello everyone, I solved my own question. I'm posting this here so that anyone facing this issue can get some idea.
There is a good reference here on how to read binary data from the AsyncFileUpload Control...http://forums.asp.net/t/1520272.aspx/1[^].
All i needed to do was to assign the stream obj with the FileContent of the Async control...like
System.IO.Stream myStream;
 
        myStream = AsyncFileUpload1.FileContent;
        myStream.Position = 0;
 
        System.IO.BinaryReader br = new System.IO.BinaryReader(myStream);
 
        byte[] bytImageBytes = br.ReadBytes((int)myStream.Length);
and set the starting position of the stream to 0 and use a binary reader to read the binary values int a byte array.
I still haven't figured out why my initial code did not work??....i.e the inputstream of the async control....Please, share if you know the reason why.
Thanks,
Mlimbu
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,580
1 OriginalGriff 8,370
2 Peter Leow 4,859
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100