wrapper function for uploading files

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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.PostedFile Is Nothing) Then
    Dim tw As TextWriter
    Dim r As HttpResponse = New HttpResponse(tw)
    Try
    inputFile.PostedFile.SaveAs(destination)
    success = True
    Catch exc As Exception
    ' r is a reference to httpResponse Object
    r.Write("Execption Message: " & exc.Message.ToString() &
    "<br />")
    r.Write("Execption Source: " & exc.Source.ToString())
    r.Write("Execption Stack: " & exc.StackTrace.ToString())
    success = False
    End Try
    Return success
    End If
    End Function


    Brian Pittman Guest

  2. Similar Questions and Discussions

    1. Problems: uploading files have Chinese named files
      Hi everyone, I try to upload file by using asp.net, code like that: <script language="VB" runat="server"> Sub Upload(Source As Object, e As...
    2. [PHP] Uploading files via SSH
      Google for "Failed to scan directories. Error 6" (including quotes). First result is the ssh.com faq, which has a link to...
    3. Wrapper files.
      Hello, I have managed to install db2 with all the Federated database bells and whistles, but when I go to create a wrapper it is unable to find the...
    4. Uploading files in C#
      why don't you examine the extension of the file being loaded. "Ian Walsh" <ianwalsh@hbosplc.com> wrote in message...
    5. Uploading Files - help please
      When you use: <Input ID="ImageUpload" Type="File" visible="false" RunAt="Server"> Is their any way you can make a starting value appear inside...
  3. #2

    Default 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
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.

    "Brian Pittman" <bpittman@nlta.nf.ca> wrote in message
    news:ega0%23crVDHA.1072@TK2MSFTNGP12.phx.gbl...
    > 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.PostedFile Is Nothing) Then
    > Dim tw As TextWriter
    > Dim r As HttpResponse = New HttpResponse(tw)
    > Try
    > inputFile.PostedFile.SaveAs(destination)
    > success = True
    > Catch exc As Exception
    > ' r is a reference to httpResponse Object
    > r.Write("Execption Message: " & exc.Message.ToString()
    &
    > "<br />")
    > r.Write("Execption Source: " & exc.Source.ToString())
    > r.Write("Execption Stack: " &
    exc.StackTrace.ToString())
    > success = False
    > End Try
    > Return success
    > End If
    > End Function
    >
    >

    Kevin Spencer Guest

  4. #3

    Default 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.NullReferenceException: 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.PostedFile 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$prVDHA.2012@TK2MSFTNGP10.phx.gbl...
    > 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
    > [url]http://www.takempis.com[/url]
    > Complex things are made up of
    > lots of simple things.
    >
    > "Brian Pittman" <bpittman@nlta.nf.ca> wrote in message
    > news:ega0%23crVDHA.1072@TK2MSFTNGP12.phx.gbl...
    > > 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.PostedFile Is Nothing) Then
    > > Dim tw As TextWriter
    > > Dim r As HttpResponse = New HttpResponse(tw)
    > > Try
    > > inputFile.PostedFile.SaveAs(destination)
    > > success = True
    > > Catch exc As Exception
    > > ' r is a reference to httpResponse Object
    > > r.Write("Execption Message: " &
    exc.Message.ToString()
    > &
    > > "<br />")
    > > r.Write("Execption Source: " &
    exc.Source.ToString())
    > > r.Write("Execption Stack: " &
    > exc.StackTrace.ToString())
    > > success = False
    > > End Try
    > > Return success
    > > End If
    > > End Function
    > >
    > >
    >
    >

    Brian Pittman Guest

  5. #4

    Default 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
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.


    "Brian Pittman" <bpittman@nlta.nf.ca> wrote in message
    news:e0EoAzrVDHA.2352@TK2MSFTNGP12.phx.gbl...
    > 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.NullReferenceException: 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.PostedFile 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$prVDHA.2012@TK2MSFTNGP10.phx.gbl...
    > > 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
    > > [url]http://www.takempis.com[/url]
    > > Complex things are made up of
    > > lots of simple things.
    > >
    > > "Brian Pittman" <bpittman@nlta.nf.ca> wrote in message
    > > news:ega0%23crVDHA.1072@TK2MSFTNGP12.phx.gbl...
    > > > 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.PostedFile Is Nothing) Then
    > > > Dim tw As TextWriter
    > > > Dim r As HttpResponse = New HttpResponse(tw)
    > > > Try
    > > > inputFile.PostedFile.SaveAs(destination)
    > > > success = True
    > > > Catch exc As Exception
    > > > ' r is a reference to httpResponse Object
    > > > r.Write("Execption Message: " &
    > exc.Message.ToString()
    > > &
    > > > "<br />")
    > > > r.Write("Execption Source: " &
    > exc.Source.ToString())
    > > > r.Write("Execption Stack: " &
    > > exc.StackTrace.ToString())
    > > > success = False
    > > > End Try
    > > > Return success
    > > > End If
    > > > End Function
    > > >
    > > >
    > >
    > >
    >
    >

    Kevin Spencer Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139