A
Al Smith
I need help in implementing proper error handling.
I am trying to upload a file based on the sample code below. The code works
well except if the file selected is too big.
I do know about the maxRequestLength parameter of the <httpRuntime> section
and that works as expected. What I want is to enforce a max file size but
haven't been able to trap the error thrown when the file is too large and
that's where I could use some help.
Thanks
The main code is shown below and was copied from one of MS samples.
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)
If Text1.Value = "" Then
Span1.InnerHtml = "Error: you must enter a file name"
Return
End If
If Not (File1.PostedFile Is Nothing) Then
Try
File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
Text1.Value & "</b> on the Web server"
Catch exc As Exception
Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
Text1.Value & "</b><br>" & exc.ToString()
End Try
End If
End Sub 'Button1_Click
</script>
<form enctype="multipart/form-data" runat="server">
Select File to Upload:
<input id="File1"
type="file"
maxlength="30"
runat="server"/>
<p/>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server"/>
<p/>
<span id="Span1"
style="font: 8pt verdana;"
runat="server" />
<p/>
<input type="button"
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>
I am trying to upload a file based on the sample code below. The code works
well except if the file selected is too big.
I do know about the maxRequestLength parameter of the <httpRuntime> section
and that works as expected. What I want is to enforce a max file size but
haven't been able to trap the error thrown when the file is too large and
that's where I could use some help.
Thanks
The main code is shown below and was copied from one of MS samples.
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)
If Text1.Value = "" Then
Span1.InnerHtml = "Error: you must enter a file name"
Return
End If
If Not (File1.PostedFile Is Nothing) Then
Try
File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
Text1.Value & "</b> on the Web server"
Catch exc As Exception
Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
Text1.Value & "</b><br>" & exc.ToString()
End Try
End If
End Sub 'Button1_Click
</script>
<form enctype="multipart/form-data" runat="server">
Select File to Upload:
<input id="File1"
type="file"
maxlength="30"
runat="server"/>
<p/>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server"/>
<p/>
<span id="Span1"
style="font: 8pt verdana;"
runat="server" />
<p/>
<input type="button"
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>