Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the following code I get an exception like 'Use the new keyword to create an object instance' at line 'int fileLength1 = PhotoUpload.PostedFile.ContentLength'. How to solve
it?

C#
string fileName = ImagePreview.ImageUrl;
      int fileLength1 = PhotoUpload.PostedFile.ContentLength;
      byte[] imageBytes = new byte[fileLength1];
      PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength1);
Posted
Comments
Dominic Burford 11-Nov-14 11:08am    
Check if the object references for PostedFile and ContentLength both exist before you reference them.

From just that fragment, we can't really tell.
So you need to start by looking at what's going on with the debugger.
Put a breakpoint at the start of the method and step through looking at the various variables. The chances are that either PhotoUpload is null, or it's PostedFile property is. Why they are will depend on your code elsewhere, which we don;t have access to. But when you know which, you can start looking at where it should be set, and that should tell you about why it hasn't been.

Sorry, but without having your code running in your system and doing exactly what you did to get that far there really isn't anything else we can do.
 
Share this answer
 
My guess is there is no file. You need to check if a file was uploaded, only then access the PostedFile property. Modify your code like this:
C#
if(PhotoUpload.HasFile)     
{
      int fileLength1 = PhotoUpload.PostedFile.ContentLength;
      byte[] imageBytes = new byte[fileLength1];
      PhotoUpload.PostedFile.InputStream.Read(imageBytes, 0, fileLength1);
}
 
Share this answer
 
Check if the object references for PostedFile and ContentLength both exist before you reference them.
 
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