473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 36433
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Seong Jin, Cho | last post by:
Hi. Is there a way to get an unbuffered InputStream from Process? I found out that java.lang.Win32Process and java.lang.UNIXProcess both wraps the stdout with java.io.BufferedInputStream, but...
1
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new...
4
by: | last post by:
Hi! It seems the HttpPostedFile object can't save a file to a destination withing the web's structure. I want to upload pictures from a page and store them in a subfolder to the web folder...
1
by: terrorix | last post by:
I want to save uploaded file to disk. I have this construction: HttpPostedFile myFile = ((HttpRequest)Request).Files; if (myFile != null) { string fn =...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
5
by: MSDN | last post by:
Does anyone know why I am getting Invalid cast exception??? For Each oFile As System.web.HttpPostedFile In Request.Files ....... etc..... Next I Checked that
1
by: samtilden | last post by:
I want to print out some tracing messages from Global.asax.cs/Application_BeginRequest(). I easily got: System.Web.HttpContext.Current.Request.RawUrl, and...
1
by: Ole | last post by:
I can't get the sense of the ReadTimeout property in the serial port - what is the function of it? the serialport.Read returns immediately no matter how many bytes you ask it to read and no matter...
1
by: Ralph Watermann | last post by:
Hi, we upload an Excel-file via a FileUpload-Control into an HttpPostedFile object. Now we want to access the contained data directly without saving it to a temp file. Is this possible? So...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.