Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

C#
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

This may work for you.

Instead of trying to read the whole file all at once, read it in chunks.
C#
myStream = AsyncFileUpload1.PostedFile.InputStream;
int bufferSize = 1024;
long bytesRead = 0;
int curPos = 0;

while (bytesRead < nFileLength)
{
    bytesRead += myStream.Read(bytImageBytes, curPos, bufferSize);
    curPos += bufferSize;
}
 
Share this answer
 
v2
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
C#
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
 
Share this answer
 

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