Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > System.web.HttpPostedFile

Reply
Thread Tools

System.web.HttpPostedFile

 
 
MSDN
Guest
Posts: n/a
 
      05-09-2006
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






 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      05-10-2006
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!
--
Nathan Sokalski

http://www.nathansokalski.com/

"MSDN" <> wrote in message
news:...
> 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
>
>
>
>
>
>



 
Reply With Quote
 
 
 
 
MSDN
Guest
Posts: n/a
 
      05-10-2006
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



"Nathan Sokalski" <> wrote in message
news:eJoRDV%...
>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!
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
> "MSDN" <> wrote in message
> news:...
>> 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
>>
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      05-10-2006
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!
--
Nathan Sokalski

http://www.nathansokalski.com/

"MSDN" <> wrote in message
news:...
> 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
>
>
>
> "Nathan Sokalski" <> wrote in message
> news:eJoRDV%...
>>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!
>> --
>> Nathan Sokalski
>>
>> http://www.nathansokalski.com/
>>
>> "MSDN" <> wrote in message
>> news:...
>>> 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
>>>
>>>
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
MSDN
Guest
Posts: n/a
 
      05-10-2006
Thanks for your input,
I can work around it by not using For Each...

Thanks again,

SA


"Nathan Sokalski" <> wrote in message
news:...
>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!
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
> "MSDN" <> wrote in message
> news:...
>> 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
>>
>>
>>
>> "Nathan Sokalski" <> wrote in message
>> news:eJoRDV%...
>>>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!
>>> --
>>> Nathan Sokalski
>>>
>>> http://www.nathansokalski.com/
>>>
>>> "MSDN" <> wrote in message
>>> news:...
>>>> 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
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
paul.rousou.adams@googlemail.com
Guest
Posts: n/a
 
      05-25-2006
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...

 
Reply With Quote
 
fsimon fsimon is offline
Junior Member
Join Date: Oct 2009
Posts: 1
 
      10-05-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57