This is a migrated thread and some comments may be shown as answers.

Attachments not storing properly in database

1 Answer 86 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Baal
Top achievements
Rank 1
Baal asked on 20 May 2010, 09:23 PM
I have radUpload control in my page from which users can only upload images.

<telerik:RadUpload ID="ruImage" runat="server" InitialFileInputsCount="1" ControlObjectsVisibility="None" AllowedMimeTypes="image/bmp,image/gif,image/jpeg,image/pjpeg,image/tiff,image/x-png" />  

I am using the following code in my codebehind to upload the images to the database.
if (ruImage.UploadedFiles.Count > 0) 
    AppImages oImage = null
 
    foreach (UploadedFile theFile in ruImage.UploadedFiles) 
    { 
        string fileName = theFile.FileName; 
        fileName = fileName.Substring(fileName.LastIndexOf(@"\")).Replace(@"\"""); 
 
        byte[] myBytes = new byte[theFile.ContentLength]; 
        theFile.InputStream.Read(myBytes, 0, theFile.ContentLength); 
 
 
        oImage = new AppImages(); 
 
        oImage.AppId = appId; 
        oImage.FileName = fileName; 
        oImage.FileSize = theFile.ContentLength; 
        oImage.FileType = theFile.ContentType; 
        oImage.Attachment = myBytes; 
        oImage.LastUpdatedBy = UserName; 
        oImage.Insert(); 
 
        oImage = null
    } 

The column Attachment in the AppImages table is of type IMAGE. When I debug the code, everything is good as it should be except the content of myBytes. every single element of the array will have 0 as its value. Even when I check the database (I'm using SQL Server 2008), the content of the Attachment column will be 0x000...

Now when I try to load the images to the grid (I have GridBinaryImageColumn), I get the following error from the IE8 debugger:
Error: Sys.WebForms.PageRequestManagerServerErrorException: The provided binary data may not be valid image or may contains unknown header

When I force the page to load, I get the following error:
Parameter is not valid.  
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
 
Exception Details: System.ArgumentException: Parameter is not valid. 
 
Source Error:  
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.   
 
Stack Trace:  
 
 
[ArgumentException: Parameter is not valid.] 
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +430 
   System.Drawing.Image.FromStream(Stream stream) +14 
   Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +77 
 
[ArgumentException: The provided binary data may not be valid image or may contains unknown header] 
   Telerik.Web.UI.ImageFilterHelper.CreateImgFromBytes(Byte[] image) +219 
   Telerik.Web.UI.RadBinaryImage.AutoAdjustImageElementSize(Byte[] dataValue) +26 
   Telerik.Web.UI.RadBinaryImage.ProcessImageData() +106 
   Telerik.Web.UI.RadBinaryImage.OnPreRender(EventArgs e) +27 
   System.Web.UI.Control.PreRenderRecursiveInternal() +108 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Control.PreRenderRecursiveInternal() +224 
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394 
 
  
 
 
-------------------------------------------------------------------------------- 
Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927  

I've searched the net for this error but coulnd't find any help. Have you guys seen anything like this?

Thanks.



1 Answer, 1 is accepted

Sort by
0
Baal
Top achievements
Rank 1
answered on 21 May 2010, 08:25 PM
The problem while uploading the file was that somehow the position of the InputStream wasn't at 0. So I added theFile.InputStream.Position = 0 before populating the byte array with the data from the InputStream and it worked fine.
Tags
Upload (Obsolete)
Asked by
Baal
Top achievements
Rank 1
Answers by
Baal
Top achievements
Rank 1
Share this question
or