469,898 Members | 1,537 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

HttpPostedFile.InputStream.ReadTimeout

Hello,

Iīve a strange problem with my HttpPostedFile object.

I coded a File Upload where the user can upload a zip file. My code
looks like this:

HttpFileCollection files = Page.Request.Files;
HttpPostedFile file = files[0];

When I set a breakpoint at the second line and run the debugger,
the debugger tolds me in the properties of my object "file":

ReadTimeout = "file.InputStream.ReadTimeout" throws an Exception of
Typ "System.InvalidOperationException"

WriteTimeout = "file.InputStream.WriteTimeout"throws an Exception of
Typ "System.InvalidOperationException"
Does anybody have an idea what happens?
Regards,

Martin
Dec 3 '07 #1
4 34602
This is normal and just means that the stream doesn't support
read/write timeouts.

Most streams don't; the base-class (System.IO.Stream) has "CanTimeout"
return false, and "ReadTimeout" / "WriteTimeout" throw an Exception.
The System.Web.HttpPostedFile functionality uses a
System.Web.HttpInputStream which doesn't alter this behavior.

Is this causing a problem, or just out of interest?

Marc
Dec 3 '07 #2
Hi Marc,

thank you!

On 3 Dez., 13:57, "Marc Gravell" <marc.grav...@gmail.comwrote:
This is normal and just means that the stream doesn't support
read/write timeouts.
Well... in my opinion, itīs not a good coding style, if an Exception
is "normal".
Is this causing a problem, or just out of interest?
I am having trouble with reading the stream into a byte-array and I am
not sure,
if the Exception is the cause of the problem.

My code looks like this:

BufferedStream bs = new BufferedStream(file.InputStream);
int length = System.Convert.ToInt32(bs.Length);
byte[] zipFile = new byte[length];

// wandle Zip-File in byte[] um
int offset = 0;
int remaining = length;
while (remaining 0)
{
int read = bs.Read(zipFile, offset, remaining);
if (read <= 0)
throw new EndOfStreamException(String.Format("End of
stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
Before entering the while-loop the values of my variables are int
remaining = 1189816 = length
In the first iteration of the while loop, the Read method reads 7109
bytes (int read = 7109).
After this, the variables have values int remaining = 1182707 and int
offset = 7109. In the second while-loop, the Read method
(bs.Read(zipFile, 7109, 1182707) does not read any byte(!!!), which
means int read = 0.

Do you have an idea why that happens?
Regards,
Martin
Dec 3 '07 #3
On 3 Dez., 14:38, macap.use...@googlemail.com wrote:
I am having trouble with reading the stream into a byte-array and I am
not sure,
if the Exception is the cause of the problem.

My code looks like this:

BufferedStream bs = new BufferedStream(file.InputStream);
int length = System.Convert.ToInt32(bs.Length);
byte[] zipFile = new byte[length];

// wandle Zip-File in byte[] um
int offset = 0;
int remaining = length;
while (remaining 0)
{
int read = bs.Read(zipFile, offset, remaining);
if (read <= 0)
throw new EndOfStreamException(String.Format("End of
stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;

}
When I store the HttpPostedFile temporary and read it with a
FileStream after that,
everything works fine:

file.SaveAs("C:\\tmp\\test.zip"); // file is an object of
HttpPostedFile
FileStream fs = new FileStream(@"C:\\tmp\\testtesttest.zip",
FileMode.Open, FileAccess.Read);

// Create a byte array of file stream length
byte[] zipFile = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs.Read(zipFile, 0, System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();
Does anybody know a better solution without storing the HttpPostedFile
into a tmp Folder?
Bye,
Martin
Btw: is there a way to get the path of the temp-folder as String?
I only found "System.Environment.SpecialFolder.InternetCach e" which
does not give any path as string.
Dec 3 '07 #4
Well... in my opinion, itīs not a good coding style, if an Exception
is "normal".
The exception isn't "normal", and doesn't relate to /any/ coding
style - it only happened because you are looking at the object via the
debugger. The correct way of using those properties is to check the
CanTimout first to see if they make sense; if it returns false you
shouldn't even be /looking/ at these values, so it is entirely
reasonble for it to throw an exception. The debugger doesn't care
about that, and simply asks for the value of every property.

For info, if you are working with zip, you might want to look at
SharpZipLib (ICSharpCode) - this should allow you to work with the
internals quite easily.

Marc
Dec 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Seong Jin, Cho | last post: by
1 post views Thread by Charles.Deisler | last post: by
4 posts views Thread by | last post: by
reply views Thread by bvasanth123 | last post: by
5 posts views Thread by MSDN | last post: by
1 post views Thread by samtilden | last post: by
1 post views Thread by Ralph Watermann | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.