Byte[] Conversion

S

shapper

Hello,

I am receiving a HttpPostedFileBase from a web form.
When I debug it its ContentLength is 25310.

Then I try to convert it to Byte[]:

public Byte[] Convert(HttpPostedFileBase source) {

Byte[] destination = new Byte[source.ContentLength];
source.InputStream.Read(destination, 0, source.ContentLength);
return destination;

} // Convert

And at the end I get a byte [25310] but all bytes are 0.

Any idea what I might be doing wrong?

Thank You,
Miguel
 
P

Peter Duniho

Hello,

I am receiving a HttpPostedFileBase from a web form.
When I debug it its ContentLength is 25310.

Then I try to convert it to Byte[]:

public Byte[] Convert(HttpPostedFileBase source) {

Byte[] destination = new Byte[source.ContentLength];
source.InputStream.Read(destination, 0, source.ContentLength);
return destination;

} // Convert

And at the end I get a byte [25310] but all bytes are 0.

_All_ of the bytes? Even the first one?
Any idea what I might be doing wrong?

My first guess would be that you are failing to read all the way to the
end of the stream. The Read() method is not required to read as many
bytes as you ask for; you have to check the return value to find out how
many bytes are _actually_ read.

But as you know, without a concise-but-complete code example, no...it's
not possible to say for sure what's wrong.

Pete
 
S

shapper

My first guess would be that you are failing to read all the way to the  
end of the stream.  The Read() method is not required to read as many  
bytes as you ask for; you have to check the return value to find out how  
many bytes are _actually_ read.

So I need to implement a loop and read until it reaches the end?
But as you know, without a concise-but-complete code example, no...it's  
not possible to say for sure what's wrong.

Yes I know ... I didn't post it because I can't find where the problem
is ...

Sometimes it works sometimes it doesn't ... Which is really strange.
I am still trying to figure it out.
 
P

Patrice

Yes I know ... I didn't post it because I can't find where the problem

If it works, there is no need to post code. The problem is that for now it
could happen somewhere else than in those 3 lines.

A standard approach is :
- simplify your code as much as possible. During this process you may find
what is is (suddenly it works and you realize that...)
- if it still doesn't work and odn"t find from the simplified code, then
you'll be able to post the shortest possible amount of code that shows the
problem and new eyes will perhaps catch this...
Sometimes it works sometimes it doesn't ... Which is really strange.

Bad case, in this case the code is perhps not enough.

Do you repro this yourself often or is it when this code runs for your end
users ? If other users you may want to check IsClientDisconnected perhaps ?
Also it returns a value. It would allow to check that you read something.
(for now I would say that actualy you don't read anything and the array is
kept "zeroed". Checking the return value would allow to know.

So I would start :
- check the return value
- if the client is still connected or closed the browser before the upload
ends...
- make sure you don't share anything (static) in which case you'll have
problem when multiple users are running your code...
 
P

Patrice

To make it clear, the read method returns a value. Checking this would made
clear either that you get nothing from the stream or that the array is
processed in a way that it is sometimes destroyed (not sure how you know the
array is all "zeroed").
 
S

shapper

To make it clear, the read method returns a value. Checking this would made
clear either that you get nothing from the stream or that the array is
processed in a way that it is sometimes destroyed (not sure how you know the
array is all "zeroed").

--
Patrice

If it works, there is no need to post code. The problem is that for nowit
could happen somewhere else than in those 3 lines.
A standard approach is :
- simplify your code as much as possible. During this process you may find
what is is (suddenly it works and you realize that...)
- if it still doesn't work and odn"t find from the simplified code, then
you'll be able to post the shortest possible amount of code that shows the
problem and new eyes will perhaps catch this...
Bad case, in this case the code is perhps not enough.
Do you repro this yourself often or is it when this code runs for your end
users ? If other users you may want to check IsClientDisconnected perhaps
? Also it returns a value. It would allow to check that you read
something. (for now I would say that actualy you don't read anything and
the array is kept "zeroed". Checking the return value would allow to know.
So I would start :
- check the return value
- if the client is still connected or closed the browser before the upload
ends...
- make sure you don't share anything  (static) in which case you'll have
problem when multiple users are running your code...

I debugged checked the content of "destination" in "return
destination" and I get all bytes 0 and size 28963.

I tried the following:

Int32 length = source.ContentLength;
Byte[] destination = new Byte[source.ContentLength];
Int32 output = source.InputStream.Read(destination, 0,
source.ContentLength);
return destination;

length is 28963 and output is 0.

Any idea?

(Note: today never works ... yesterday I think it worked a few times
but I was so tired that I start to doubt myself :) )
 
P

Peter Duniho

I debugged checked the content of "destination" in "return
destination" and I get all bytes 0 and size 28963.

I tried the following:

Int32 length = source.ContentLength;
Byte[] destination = new Byte[source.ContentLength];
Int32 output = source.InputStream.Read(destination, 0,
source.ContentLength);
return destination;

length is 28963 and output is 0.

A Stream returns 0 from the Read() method when the end-of-stream has been
reached and there are no more bytes to be read.

In your case, this could mean at least one of three things:

-- Some other code somewhere has already read through to the end of
the stream
-- The stream is being closed prematurely, resulting in the
end-of-stream occurring before any bytes have been read
-- Some combination of the above two

Again, without a proper code example, we can't help you narrow down the
details.

It is true that you should read in a loop, but you should do that until
the Read() method returns 0. So if it's returning 0 right away, fixing
that particular aspect of the code isn't going to help (that is, you do
need to make that fix, but you still have other problems).

Pete
 
P

Patrice

In addition to Peter and as it seems your code never works I would restart
fresh with :
- a posting page
- upload a short text file
- and see if you get the expected text

If it doesn't work post the code. If it's work keep building on this.

It's hard to say more until we see the minimal amount of code that allows us
to repro the problem... BTW is this your web page or some other web page
that is supposed to send you those data ? If another page, especially non
ASP.NET, the other page could perhaps miss the proper enctype attribute...
 
S

shapper

I am receiving a HttpPostedFileBase from a web form.
When I debug it its ContentLength is 25310.
Then I try to convert it to Byte[]:
 public Byte[] Convert(HttpPostedFileBase source) {
     Byte[] destination = new Byte[source.ContentLength];
     source.InputStream.Read(destination, 0, source.ContentLength);
     return destination;
   } // Convert
And at the end I get a byte [25310] but all bytes are 0.

When you declare the destination array the runtime will set all its
bytes to zero.  Either you are not reading anything into the arra, or
what you are reading is all zeros.

You must look at the return from read to see how many bytes you are
actually reading.

rossum


Any idea what I might be doing wrong?
Thank You,
Miguel

Hello,

I found the problem!
I followed the suggestions to try to isolate the problem ...
After a while I understood that when validating the file with
ImageHeight and ImageWidth validator everything worked.

This is why it worked sometimes and didn't work other times.
In my CSM I am uploading sometimes images other times pdf's.
When uploading the pdf the ImageValidators are not used.

In ImageHeightValidator I have something like:

Boolean valid;
if (context.PropertyValue.ContentLength == 0)
valid = true;
else {
Bitmap image = new Bitmap(context.PropertyValue.InputStream);
valid = image.Height >= _minimum && image.Height <= _maximum;
}

So I added on the Resolver the following:

source.InputStream.Position = 0;

Or maybe more correct to add on the ImageHeightValidator:

context.PropertyValue.InputStream.Position = 0;

Thank You,
Miguel
 
P

Peter Duniho

[...]
So I added on the Resolver the following:

source.InputStream.Position = 0;

Or maybe more correct to add on the ImageHeightValidator:

context.PropertyValue.InputStream.Position = 0;

Right. In other words, just as I suggested, you had code elsewhere that
was already reading through the entire stream.

And this is a great example of why it's so important to post
concise-but-complete code examples. Very often, in the process of trying
to construct the code example, you can find the problem yourself, avoiding
the need to post a vague, unanswerable question at all.

Pete
 
S

shapper

[...]
So I added on the Resolver the following:
source.InputStream.Position = 0;
Or maybe more correct to add on the ImageHeightValidator:
context.PropertyValue.InputStream.Position = 0;

Right.  In other words, just as I suggested, you had code elsewhere that  
was already reading through the entire stream.

And this is a great example of why it's so important to post  
concise-but-complete code examples.  Very often, in the process of trying  
to construct the code example, you can find the problem yourself, avoiding  
the need to post a vague, unanswerable question at all.

Yes Pete, I completely agree but I wasn't able to post the complete
code because I had no idea where the problem was ...

In fact when I first started the post I was really convinced the
problem was on my code of converting HttpPostedFileBase to Byte[].
 
P

Peter Duniho

Yes Pete, I completely agree but I wasn't able to post the complete
code because I had no idea where the problem was ...

Right. That's the whole point. If you have no idea where the problem is,
without creating a proper code example, you have no way of knowing that
you've posted code that would allow others to figure out where the problem
is. How do you expect any of us to answer a question if the question
doesn't actually include the code that's causing the problem?

At the same time, if you put in the effort to create a proper code
example, at a minimum that means the code example you will eventually post
will necessarily include the part of the code where the problem is. And
more importantly, by going through the exercise yourself, you will often
find the exact problem yourself, avoiding the need to post the question at
all.

In other words, while I understand that you had no idea where the problem
was, that is not in fact a reason to not post a concise-but-complete code
example, never mind does it imply you can't. It is _always_ possible to
post a concise-but-complete code example that reliably demonstrates the
problem; the only question is how much effort the person posting the
question is willing to put in. (And "garbage-in, garbage-out" applies
here...if you don't put effort into the question, don't expect others to
put effort into the answer).

Pete
 
S

shapper

[...]
So I added on the Resolver the following:
source.InputStream.Position = 0;
Or maybe more correct to add on the ImageHeightValidator:
context.PropertyValue.InputStream.Position = 0;
Right.  In other words, just as I suggested, you had code elsewhere that  
was already reading through the entire stream.
And this is a great example of why it's so important to post  
concise-but-complete code examples.  Very often, in the process of trying  
to construct the code example, you can find the problem yourself, avoiding  
the need to post a vague, unanswerable question at all.
Yes Pete, I completely agree but I wasn't able to post the complete
code because I had no idea where the problem was ...

That is the point.  In order to cut down the real code to something
postable you need to cut chunks out or turn methods into stubs.  In
the process of doing that you may well find that the problem
disappears and so you have then localised the problem to the last bit
of code that you cut out or stubbed.


In fact when I first started the post I was really convinced the
problem was on my code of converting HttpPostedFileBase to Byte[].

If you cannot solve a problem yourself, one of the reasons may well be
that you are looking in the wrong place.

rossum

Yes, you are completely right ... But bear with me on this one ... I
was going crazy. I was for hours trying to find the problem I posted
in a small hope that error was on my conversion code and that somebody
might see it ...
But immediately I went back to it ... :)

Thank You,
Miguel
 
B

Ben Voigt [C++ MVP]

In other words, while I understand that you had no idea where the problem
was, that is not in fact a reason to not post a concise-but-complete code
example, never mind does it imply you can't. It is _always_ possible to
post a concise-but-complete code example that reliably demonstrates the
problem; the only question is how much effort the person posting the

It isn't always possible to make a concise example, but complete,
definitely. Of course time should be spent trying to make the repro case as
simple as possible, usually this leads to understanding the bug. But
sometimes the cause is a combination of several factors and there is no less
complicated case where the problem occurs.
 
P

Peter Duniho

It isn't always possible to make a concise example, but complete,
definitely. [...]

That depends on your definition of "concise". Mine is "the least amount
of code to demonstrate the problem". In other words, even if it's 10,000
lines of code, that's "concise" as long as removing even a single line of
code from that example would prevent the code from demonstrating the
problem.

Other people prefer "concise" to be more absolute. YMMV. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Convert to Byte 5
HttpPostedFileBase and byte[] 9
Byte Array 4
Resize Image 2
Fast byte array compare for ordering purposes 5
Enum Question 1
C# Byte/Short Mathematics 8
How to Copy like C++? 2

Top