System.web.HttpPostedFile

M

MSDN

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

Request.Files(0).GetType.tostring = "System.web.HttpPostedFile"

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

Thanks,

SA
 
N

Nathan Sokalski

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.HtmlInputFile 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!
 
M

MSDN

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.HttpPostedFile 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
 
N

Nathan Sokalski

I wish I could make other suggestions, but because I have never used
System.web.HttpPostedFile 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.HttpPostedFile, 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!
 
P

paul.rousou.adams

The 'bad cast' is due to the usage of Request.Files which needs to be
cast to an HttpFileCollection.

E.g.

Dim collFiles As HttpFileCollection = Request.Files
Dim objFile As HttpPostedFile

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

Hope that helps...
 
Joined
Oct 5, 2009
Messages
1
Reaction score
0
Unit test method post httpPostedFileBase

Hi,

Please, I have a method that receives a Post file. But I can't do unit test because it must create a HttpPostedFileBase to send as parameter. Has anyone experienced this?
signature :
public void SaveFile(HttpPostedFileBase File)


Tnks

Hugs
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top