System.web.HttpPostedFile

Discussion in 'ASP .Net' started by MSDN, May 9, 2006.

  1. MSDN

    MSDN Guest

    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
     
    MSDN, May 9, 2006
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
    >
    >
     
    Nathan Sokalski, May 10, 2006
    #2
    1. Advertising

  3. MSDN

    MSDN Guest

    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
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >
     
    MSDN, May 10, 2006
    #3
  4. 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
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Nathan Sokalski, May 10, 2006
    #4
  5. MSDN

    MSDN Guest

    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
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    MSDN, May 10, 2006
    #5
  6. MSDN

    Guest

    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...
     
    , May 25, 2006
    #6
  7. MSDN

    fsimon

    Joined:
    Oct 5, 2009
    Messages:
    1
    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
     
    fsimon, Oct 5, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    1
    Views:
    1,875
    Kevin Spencer
    Oct 27, 2003
  2. Guest

    HttpPostedFile

    Guest, Nov 11, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,097
    Kevin Spencer
    Nov 11, 2003
  3. huan

    HttpPostedFile problem

    huan, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    732
    vMike
    Jan 5, 2004
  4. Al Smith

    HttpPostedFile.SaveAs

    Al Smith, Aug 30, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    3,709
    Steven Cheng[MSFT]
    Sep 1, 2004
  5. Bruno Piovan

    Re: HttpPostedFile

    Bruno Piovan, Sep 14, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    Bruno Piovan
    Sep 14, 2004
Loading...

Share This Page