Our classic ASP application uses one .aspx page to upload an image for each client.
I’m not familiar with ASP.net, so this page is difficult for me to understand.
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="VB" RunAt="Server">
Sub Page_Load(Sender As Object, e As EventArgs)
Span1.InnerHtml = "You may not upload a file larger than 250 Kilobytes. Your image must be in JPG format.<br />Please crop your image to 400 pixels high and 300 pixels wide, so that no distortion will occur when displayed.<br /><br />"
End Sub
Sub Upload_Click(Sender As Object, e As EventArgs)
Dim DistributorID As Integer
DistributorID = request.QueryString("distID")
' Display properties of the uploaded file
'Let us recover only the file name from its fully qualified path at client
Dim strFileName As String
strFileName = MyFile.PostedFile.FileName
Dim c As String = CStr(DistributorID) & "." & split(System.IO.Path.GetFileName(strFileName), ".")(1) ' The file name is the distid + the extension
'Let us Save uploaded file to server
If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(0)) = "" Then
Span1.InnerHtml = "Please select a Photo.<br /><br />"
End If
If LCase(Split(System.IO.Path.GetFileName(strFileName), ".")(1)) <> "jpg" Then
Span1.InnerHtml = "Invalid file format."
Else
If MyFile.PostedFile.ContentLength < 250000 Then
MyFile.PostedFile.SaveAs(ConfigurationSettings.AppSettings("DistImagePath") + c)
Span1.InnerHtml = "Your file imported sucessfully to the server.<br /><br />"
Else
Span1.InnerHtml = "Your file is too large to import.<br /><br />"
End If
End If
End Sub
</script>
</head>
<body>
<span id="Span1" style="color:Red;" RunAt="Server" />
<form method="Post" enctype="Multipart/Form-Data" RunAt="Server" class="stdform stdform2">
Select New Photo: <br />
<input id="MyFile" type="File" runat="Server" size="40" /><br /><br />
<span style="padding-left:25px;">
<button type="Submit" value="Import" OnServerclick="Upload_Click" RunAt="Server">Upload New Photo</button>
</span>
</form>
</body>
If no file is selected and the Upload New Photo button is pressed, I get a
“Index was outside the bounds of the array.” error
How can I fix this page to not attempt to upload a photo if they haven’t selected one?
You can enclose everything in an IF as so:
Full code: