Ask a Question related to ASP.NET General, Design and Development.
-
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.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
-
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... -
[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... -
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... -
Uploading files in C#
why don't you examine the extension of the file being loaded. "Ian Walsh" <ianwalsh@hbosplc.com> wrote in message... -
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... -
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
[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()exc.StackTrace.ToString())> "<br />")
> r.Write("Execption Source: " & exc.Source.ToString())
> r.Write("Execption Stack: " &> success = False
> End Try
> Return success
> End If
> End Function
>
>
Kevin Spencer Guest
-
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.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...initializes> Your method has a parameter called "inputFile" which I assume is the
> uploaded file. Then, you assign it to a "New InputFile()" whichuploading> 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 forI> > files to the server? I have made an attempt on my own without success.file> > don't get any exceptions from the function but It doens't upload theexc.Message.ToString()> > 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.Source.ToString())> &> > "<br />")
> > r.Write("Execption Source: " &> exc.StackTrace.ToString())> > r.Write("Execption Stack: " &>> > success = False
> > End Try
> > Return success
> > End If
> > End Function
> >
> >
>
Brian Pittman Guest
-
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
[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...success.> 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...> initializes> > Your method has a parameter called "inputFile" which I assume is the
> > uploaded file. Then, you assign it to a "New InputFile()" which> uploading> > 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> > > files to the server? I have made an attempt on my own without> I> file> > > don't get any exceptions from the function but It doens't upload the> exc.Message.ToString()> > > 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.Source.ToString())> > &> > > "<br />")
> > > r.Write("Execption Source: " &>> > exc.StackTrace.ToString())> > > r.Write("Execption Stack: " &> >> > > success = False
> > > End Try
> > > Return success
> > > End If
> > > End Function
> > >
> > >
> >
>
Kevin Spencer Guest



Reply With Quote

