wrapper function for uploading files

B

Brian Pittman

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
 
K

Kevin Spencer

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

Brian Pittman

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
 
K

Kevin Spencer

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top