473,568 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.web.Http PostedFile

Does anyone know why I am getting Invalid cast exception???

For Each oFile As System.web.Http PostedFile In Request.Files

....... etc.....

Next

I Checked that

Request.Files(0 ).GetType.tostr ing = "System.web.Htt pPostedFile"

So what is the issue here??? I can just use a loop but ....

Thanks,

SA


May 10 '06 #1
5 4482
I don't see anything wrong with the code that you show, so I have two
questions:

1. Are you SURE the error is in the For Each line?
2. If not, what code is inside the loop?

I will admit that the only time I have ever used the HttpPostedFile class is
when uploading a file from an HtmlControls.Ht mlInputFile control, but your
code looks correct based on the documentation I found. However, I have seen
situations where the errors tell you the problem is in a line other than the
one it is in (whether that is the case here, I can't say because I haven't
seen the code you have inside the loop), so one thing I might try is testing
each line individually not inside a loop by replacing oFile with
Request.Files(0 ). If you don't get any errors doing this, put it back inside
the loop and add an extra output line or use a debugger to determine the
value of oFile during each loop cycle. You may also want to post the code
inside the loop so that others can check that as well. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
Does anyone know why I am getting Invalid cast exception???

For Each oFile As System.web.Http PostedFile In Request.Files

...... etc.....

Next

I Checked that

Request.Files(0 ).GetType.tostr ing = "System.web.Htt pPostedFile"

So what is the issue here??? I can just use a loop but ....

Thanks,

SA


May 10 '06 #2
Nathan,

I am pretty sure unless the debugger is wrong.
I stepped through the debugger and it blows up at the For Each Statement and
not inside the loop
To be sure I removed everything inside the loop and still does it.

So somehow the For Each is not accepting it.

Here is the entire Code I am left with

Dim i as integer = 0
For Each oFile As System.web.Http PostedFile In Request.Files
i += 1
Next

The error occurs at the For Each
Is there anything else I can test or do? Is this a bug.

Thanks Nathan

SA

"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:eJ******** ********@TK2MSF TNGP04.phx.gbl. ..
I don't see anything wrong with the code that you show, so I have two
questions:

1. Are you SURE the error is in the For Each line?
2. If not, what code is inside the loop?

I will admit that the only time I have ever used the HttpPostedFile class
is when uploading a file from an HtmlControls.Ht mlInputFile control, but
your code looks correct based on the documentation I found. However, I
have seen situations where the errors tell you the problem is in a line
other than the one it is in (whether that is the case here, I can't say
because I haven't seen the code you have inside the loop), so one thing I
might try is testing each line individually not inside a loop by replacing
oFile with Request.Files(0 ). If you don't get any errors doing this, put
it back inside the loop and add an extra output line or use a debugger to
determine the value of oFile during each loop cycle. You may also want to
post the code inside the loop so that others can check that as well. Good
Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
Does anyone know why I am getting Invalid cast exception???

For Each oFile As System.web.Http PostedFile In Request.Files

...... etc.....

Next

I Checked that

Request.Files(0 ).GetType.tostr ing = "System.web.Htt pPostedFile"

So what is the issue here??? I can just use a loop but ....

Thanks,

SA



May 10 '06 #3
I wish I could make other suggestions, but because I have never used
System.web.Http PostedFile for very much, I've already told you about
everything I can come up with. I would try doing some searches to see what
you can find, and see if you can come up with other scenarios involving
System.web.Http PostedFile, sometimes having more different scenarios can
help you find even the smallest thing, and even the smallest piece of
knowledge can sometimes save you a huge amount of work (not always in
solving the problem you found it during, but you might thank yourself for
some of the time in the future). Maybe if you're lucky someone else will
post a little help, if nobody does in about a week and you still haven't
found an answer, you may want to try reposting, people sometimes ignore
postings more than about a week old if they aren't already involved in the
thread. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uI******** ******@TK2MSFTN GP04.phx.gbl...
Nathan,

I am pretty sure unless the debugger is wrong.
I stepped through the debugger and it blows up at the For Each Statement
and not inside the loop
To be sure I removed everything inside the loop and still does it.

So somehow the For Each is not accepting it.

Here is the entire Code I am left with

Dim i as integer = 0
For Each oFile As System.web.Http PostedFile In Request.Files
i += 1
Next

The error occurs at the For Each
Is there anything else I can test or do? Is this a bug.

Thanks Nathan

SA

"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:eJ******** ********@TK2MSF TNGP04.phx.gbl. ..
I don't see anything wrong with the code that you show, so I have two
questions:

1. Are you SURE the error is in the For Each line?
2. If not, what code is inside the loop?

I will admit that the only time I have ever used the HttpPostedFile class
is when uploading a file from an HtmlControls.Ht mlInputFile control, but
your code looks correct based on the documentation I found. However, I
have seen situations where the errors tell you the problem is in a line
other than the one it is in (whether that is the case here, I can't say
because I haven't seen the code you have inside the loop), so one thing I
might try is testing each line individually not inside a loop by
replacing oFile with Request.Files(0 ). If you don't get any errors doing
this, put it back inside the loop and add an extra output line or use a
debugger to determine the value of oFile during each loop cycle. You may
also want to post the code inside the loop so that others can check that
as well. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
Does anyone know why I am getting Invalid cast exception???

For Each oFile As System.web.Http PostedFile In Request.Files

...... etc.....

Next

I Checked that

Request.Files(0 ).GetType.tostr ing = "System.web.Htt pPostedFile"

So what is the issue here??? I can just use a loop but ....

Thanks,

SA




May 10 '06 #4
Thanks for your input,
I can work around it by not using For Each...

Thanks again,

SA
"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:OQ******** ******@TK2MSFTN GP03.phx.gbl...
I wish I could make other suggestions, but because I have never used
System.web.Htt pPostedFile for very much, I've already told you about
everything I can come up with. I would try doing some searches to see what
you can find, and see if you can come up with other scenarios involving
System.web.Htt pPostedFile, sometimes having more different scenarios can
help you find even the smallest thing, and even the smallest piece of
knowledge can sometimes save you a huge amount of work (not always in
solving the problem you found it during, but you might thank yourself for
some of the time in the future). Maybe if you're lucky someone else will
post a little help, if nobody does in about a week and you still haven't
found an answer, you may want to try reposting, people sometimes ignore
postings more than about a week old if they aren't already involved in the
thread. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uI******** ******@TK2MSFTN GP04.phx.gbl...
Nathan,

I am pretty sure unless the debugger is wrong.
I stepped through the debugger and it blows up at the For Each Statement
and not inside the loop
To be sure I removed everything inside the loop and still does it.

So somehow the For Each is not accepting it.

Here is the entire Code I am left with

Dim i as integer = 0
For Each oFile As System.web.Http PostedFile In Request.Files
i += 1
Next

The error occurs at the For Each
Is there anything else I can test or do? Is this a bug.

Thanks Nathan

SA

"Nathan Sokalski" <nj********@hot mail.com> wrote in message
news:eJ******** ********@TK2MSF TNGP04.phx.gbl. ..
I don't see anything wrong with the code that you show, so I have two
questions:

1. Are you SURE the error is in the For Each line?
2. If not, what code is inside the loop?

I will admit that the only time I have ever used the HttpPostedFile
class is when uploading a file from an HtmlControls.Ht mlInputFile
control, but your code looks correct based on the documentation I found.
However, I have seen situations where the errors tell you the problem is
in a line other than the one it is in (whether that is the case here, I
can't say because I haven't seen the code you have inside the loop), so
one thing I might try is testing each line individually not inside a
loop by replacing oFile with Request.Files(0 ). If you don't get any
errors doing this, put it back inside the loop and add an extra output
line or use a debugger to determine the value of oFile during each loop
cycle. You may also want to post the code inside the loop so that others
can check that as well. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"MSDN" <sq**********@h otmail.com> wrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
Does anyone know why I am getting Invalid cast exception???

For Each oFile As System.web.Http PostedFile In Request.Files

...... etc.....

Next

I Checked that

Request.Files(0 ).GetType.tostr ing = "System.web.Htt pPostedFile"

So what is the issue here??? I can just use a loop but ....

Thanks,

SA





May 10 '06 #5
The 'bad cast' is due to the usage of Request.Files which needs to be
cast to an HttpFileCollect ion.

E.g.

Dim collFiles As HttpFileCollect ion = Request.Files
Dim objFile As HttpPostedFile

For Each objFile In collFiles
If Not objFile.FileNam e = "" Then
objFile.SaveAs( objFile.FileNam e)
End If
Next

Hope that helps...

May 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3400
by: Jeff | last post by:
I am using the HttpPostedFile to allow users to upload files to a server farm. The SaveAs() saves to a shared network path (\\SERVER\SHARE) so that no matter which web server is taking the request, all uploaded files go to the same location. The .SaveAs() call throws the error "unknown user name or bad password". The file share referenced...
4
2917
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 (necessare to be able to show them on other pages) I can store the pics anywhere else on the server except into any directory within the web.
1
5117
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 = "c:\\Inetpub\\wwwroot\\MyWeb\\upload\\File_1"; try { myFile.SaveAs(fn); lblMsg.Text = "File was uploaded and saved successfully!";
8
8896
by: Al Smith | last post by:
Hi, I am able to use the HttpPostedFile.SaveAs() method to upload and save a file to the local web machine. However, I would really like to save the file to a VirtualDirectory. Does anyone know if this can be done and the syntax to do it? Thanks
2
7222
by: NATO24 | last post by:
Hello, I am trying to write a sub routine that I can pass a posted (image) file to. That routine will save the original file, then create a thumbnail and save it. When I try to create the image with the posted file, I get an error saying file type HttpPostedFile cannot be converted to an Image. When I save the original and try and use the...
2
12601
by: bostonnole | last post by:
I am getting this WinIOError "Insufficient system resources exist to complete the requested service" when I try to execute the following code in an ASP.NET 1.1 application uploading a very large file (140 MB). Server is Windows 2003 Standard (SP1) with 1 GB memory running IIS 6. There is plenty of free hard drive space. STACKTRACE: at...
4
3629
by: Darrel | last post by:
I'm grabbing a file from a file upload form field. This is a 'system.web.httppostedfile' I would like to modify the image (Cropping/scaling) using system.drawing.image. Is there anyway to go from 'system.web.httppostedfile' directly to 'system.drawing.image'? Normally, I'd grab the System.Drawing.Image from a file itself:
1
2520
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 far we have to to a HttpPostedFile.SaveAs(strFileName) and use a connection string to access the data via an OleDbDataAdapter: strConn = _
0
3249
by: Shaikh shahnawaz | last post by:
Hi, I have implement multiple file uploading progress bar with the help of flash and .net file is upload on my local machine but not working with server it's give error while uploading image on server. code is as follows. this is flash object: <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.