wrapper function for uploading files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Pittman

    #1

    wrapper function for uploading files

    Hi,

    I was wondering how to go about creating a wrapper function for uploading
    files to the server? I have made an attempt on my own without success. I
    don't get any exceptions from the function but It doens't upload the file
    either. Any help is appreciated.

    Brian

    '************** *******
    '** Code Snip
    '************** *******
    Function HtmlFileUpload( ByVal inputFile As HtmlInputFile, _
    ByVal destination As String) As Boolean
    '
    Dim contentLength As Integer
    inputFile = New HtmlInputFile()
    Dim success As Boolean = False
    Dim contentType As String
    If (Not inputFile.Poste dFile Is Nothing) Then
    Dim tw As TextWriter
    Dim r As HttpResponse = New HttpResponse(tw )
    Try
    inputFile.Poste dFile.SaveAs(de stination)
    success = True
    Catch exc As Exception
    ' r is a reference to httpResponse Object
    r.Write("Execpt ion Message: " & exc.Message.ToS tring() &
    "<br />")
    r.Write("Execpt ion Source: " & exc.Source.ToSt ring())
    r.Write("Execpt ion Stack: " & exc.StackTrace. ToString())
    success = False
    End Try
    Return success
    End If
    End Function


  • Kevin Spencer

    #2
    Re: wrapper function for uploading files

    Your method has a parameter called "inputFile" which I assume is the
    uploaded file. Then, you assign it to a "New InputFile()" which initializes
    to an empty HtmlInputFile. Take out that line (inputFile = New
    HtmlInputFile() ").

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

    Complex things are made up of
    lots of simple things.

    "Brian Pittman" <bpittman@nlta. nf.ca> wrote in message
    news:ega0%23crV DHA.1072@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hi,
    >
    > I was wondering how to go about creating a wrapper function for uploading
    > files to the server? I have made an attempt on my own without success. I
    > don't get any exceptions from the function but It doens't upload the file
    > either. Any help is appreciated.
    >
    > Brian
    >
    > '************** *******
    > '** Code Snip
    > '************** *******
    > Function HtmlFileUpload( ByVal inputFile As HtmlInputFile, _
    > ByVal destination As String) As Boolean
    > '
    > Dim contentLength As Integer
    > inputFile = New HtmlInputFile()
    > Dim success As Boolean = False
    > Dim contentType As String
    > If (Not inputFile.Poste dFile Is Nothing) Then
    > Dim tw As TextWriter
    > Dim r As HttpResponse = New HttpResponse(tw )
    > Try
    > inputFile.Poste dFile.SaveAs(de stination)
    > success = True
    > Catch exc As Exception
    > ' r is a reference to httpResponse Object
    > r.Write("Execpt ion Message: " & exc.Message.ToS tring()[/color]
    &[color=blue]
    > "<br />")
    > r.Write("Execpt ion Source: " & exc.Source.ToSt ring())
    > r.Write("Execpt ion Stack: " &[/color]
    exc.StackTrace. ToString())[color=blue]
    > success = False
    > End Try
    > Return success
    > End If
    > End Function
    >
    >[/color]


    Comment

    • Brian Pittman

      #3
      Re: wrapper function for uploading files

      Hey Kevin,

      Thanks for the quick response. I took out the line you specified but I
      threw an exception when I did. Here is the exception

      Exception Details: System.NullRefe renceException: Object reference not set
      to an instance of an object.

      Source Error:

      Line 30: Dim success As Boolean = False
      Line 31: Dim contentType As String
      Line 32: If (Not inputFile.Poste dFile Is Nothing) Then
      Line 33: Dim tw As TextWriter
      Line 34: Dim r As HttpResponse = New HttpResponse(tw )


      how do I fix this? Thanks again

      Brian

      "Kevin Spencer" <kevin@takempis .com> wrote in message
      news:euNw$prVDH A.2012@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Your method has a parameter called "inputFile" which I assume is the
      > uploaded file. Then, you assign it to a "New InputFile()" which[/color]
      initializes[color=blue]
      > to an empty HtmlInputFile. Take out that line (inputFile = New
      > HtmlInputFile() ").
      >
      > --
      > HTH,
      >
      > Kevin Spencer
      > Microsoft MVP
      > .Net Developer
      > http://www.takempis.com
      > Complex things are made up of
      > lots of simple things.
      >
      > "Brian Pittman" <bpittman@nlta. nf.ca> wrote in message
      > news:ega0%23crV DHA.1072@TK2MSF TNGP12.phx.gbl. ..[color=green]
      > > Hi,
      > >
      > > I was wondering how to go about creating a wrapper function for[/color][/color]
      uploading[color=blue][color=green]
      > > files to the server? I have made an attempt on my own without success.[/color][/color]
      I[color=blue][color=green]
      > > don't get any exceptions from the function but It doens't upload the[/color][/color]
      file[color=blue][color=green]
      > > either. Any help is appreciated.
      > >
      > > Brian
      > >
      > > '************** *******
      > > '** Code Snip
      > > '************** *******
      > > Function HtmlFileUpload( ByVal inputFile As HtmlInputFile, _
      > > ByVal destination As String) As Boolean
      > > '
      > > Dim contentLength As Integer
      > > inputFile = New HtmlInputFile()
      > > Dim success As Boolean = False
      > > Dim contentType As String
      > > If (Not inputFile.Poste dFile Is Nothing) Then
      > > Dim tw As TextWriter
      > > Dim r As HttpResponse = New HttpResponse(tw )
      > > Try
      > > inputFile.Poste dFile.SaveAs(de stination)
      > > success = True
      > > Catch exc As Exception
      > > ' r is a reference to httpResponse Object
      > > r.Write("Execpt ion Message: " &[/color][/color]
      exc.Message.ToS tring()[color=blue]
      > &[color=green]
      > > "<br />")
      > > r.Write("Execpt ion Source: " &[/color][/color]
      exc.Source.ToSt ring())[color=blue][color=green]
      > > r.Write("Execpt ion Stack: " &[/color]
      > exc.StackTrace. ToString())[color=green]
      > > success = False
      > > End Try
      > > Return success
      > > End If
      > > End Function
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Kevin Spencer

        #4
        Re: wrapper function for uploading files

        Let me see if I can brief you on the basic principle you seem to continually
        run up against here:

        A variable is not a class. It is a REFERENCE to a class. In other words, if
        you declare a variable such as your "tw" variable, all you've done is to
        declare a "container" for the data type that it represents (in this case, a
        TextWriter). When you first declare ("Dim") the variable, it doesn't refer
        to anything. It refers to nothing. You have to ASSIGN the variable to get it
        to refer to an actual class. Therefore, in the code which you just posted,
        you declare a variable named "tw" as a TextWriter, but assign nothing to it.
        At this point it is not referencing an instance of an object, it is
        referencing Nothing. The very next thing you did was to pass it to the
        constructor method of a HttpResponse. In other words, you passed NOTHING to
        the constructor method. NOTHING is not an instance of an object; it is
        Nothing.

        There is also a difference between declaring and instantiating (or
        assigning) an object variable. Consider the following:

        Dim x As Object ' x = Nothing
        Dim x As Object = New Object() ' x = a new instance of the Object class
        Dim x As New Object() ' x = a new instance of the Object class (the type is
        inferred from the initialization)
        Dim x As Object = SomeObject ' x is assigned to represent or point to
        SomeObject, which would be an instance of an Object

        So, while .Net allows you to combine some of these operations in a single
        statement, and infer a thing or 2, you need to be aware that the single
        statement is actually performing several things with one statement. In order
        to use variables in a .Net app, you need to do the following:

        1. Declare ("Dim") the variable (with Option Strict turned ON [recommended],
        you must declare the type as well).
        2. Assign the variable to an instance of an object. There are 2 ways to do
        this:
        a. Instantiate a New instance of an object and assign that to the
        variable
        b. Assign an existing object to the variable

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        ..Net Developer
        All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

        Complex things are made up of
        lots of simple things.


        "Brian Pittman" <bpittman@nlta. nf.ca> wrote in message
        news:e0EoAzrVDH A.2352@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hey Kevin,
        >
        > Thanks for the quick response. I took out the line you specified but I
        > threw an exception when I did. Here is the exception
        >
        > Exception Details: System.NullRefe renceException: Object reference not set
        > to an instance of an object.
        >
        > Source Error:
        >
        > Line 30: Dim success As Boolean = False
        > Line 31: Dim contentType As String
        > Line 32: If (Not inputFile.Poste dFile Is Nothing) Then
        > Line 33: Dim tw As TextWriter
        > Line 34: Dim r As HttpResponse = New HttpResponse(tw )
        >
        >
        > how do I fix this? Thanks again
        >
        > Brian
        >
        > "Kevin Spencer" <kevin@takempis .com> wrote in message
        > news:euNw$prVDH A.2012@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Your method has a parameter called "inputFile" which I assume is the
        > > uploaded file. Then, you assign it to a "New InputFile()" which[/color]
        > initializes[color=green]
        > > to an empty HtmlInputFile. Take out that line (inputFile = New
        > > HtmlInputFile() ").
        > >
        > > --
        > > HTH,
        > >
        > > Kevin Spencer
        > > Microsoft MVP
        > > .Net Developer
        > > http://www.takempis.com
        > > Complex things are made up of
        > > lots of simple things.
        > >
        > > "Brian Pittman" <bpittman@nlta. nf.ca> wrote in message
        > > news:ega0%23crV DHA.1072@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
        > > > Hi,
        > > >
        > > > I was wondering how to go about creating a wrapper function for[/color][/color]
        > uploading[color=green][color=darkred]
        > > > files to the server? I have made an attempt on my own without[/color][/color][/color]
        success.[color=blue]
        > I[color=green][color=darkred]
        > > > don't get any exceptions from the function but It doens't upload the[/color][/color]
        > file[color=green][color=darkred]
        > > > either. Any help is appreciated.
        > > >
        > > > Brian
        > > >
        > > > '************** *******
        > > > '** Code Snip
        > > > '************** *******
        > > > Function HtmlFileUpload( ByVal inputFile As HtmlInputFile, _
        > > > ByVal destination As String) As Boolean
        > > > '
        > > > Dim contentLength As Integer
        > > > inputFile = New HtmlInputFile()
        > > > Dim success As Boolean = False
        > > > Dim contentType As String
        > > > If (Not inputFile.Poste dFile Is Nothing) Then
        > > > Dim tw As TextWriter
        > > > Dim r As HttpResponse = New HttpResponse(tw )
        > > > Try
        > > > inputFile.Poste dFile.SaveAs(de stination)
        > > > success = True
        > > > Catch exc As Exception
        > > > ' r is a reference to httpResponse Object
        > > > r.Write("Execpt ion Message: " &[/color][/color]
        > exc.Message.ToS tring()[color=green]
        > > &[color=darkred]
        > > > "<br />")
        > > > r.Write("Execpt ion Source: " &[/color][/color]
        > exc.Source.ToSt ring())[color=green][color=darkred]
        > > > r.Write("Execpt ion Stack: " &[/color]
        > > exc.StackTrace. ToString())[color=darkred]
        > > > success = False
        > > > End Try
        > > > Return success
        > > > End If
        > > > End Function
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...