Reputation: 1796
Protected Sub AddFileButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
If FileUploader.HasFile Then
Dim fileSize = FileUploader.PostedFile.ContentLength
If fileSize > 1048576 Then
Info.Text = "This file exceeds the allowed file size (1 MB). Please resize the image or select another file."
Return
ElseIf fileSize < 1 Then
Info.Text = "This file does not have enough content to send. Please choose another file."
Return
Try
Dim extension = System.IO.Path.GetExtension(FileUploader.FileName)
Dim uniqueFileName = System.Guid.NewGuid.ToString() & extension
FileUploader.SaveAs("\\filepath\" & FileUploader.FileName)
Catch ex As Exception
Info.Text = "ERROR: " & ex.Message.ToString()
End Try
End If
End If
End Sub
For some reason it is not hitting the If fileSize > 1048576 Then
or the Else If fileSize < 1 Then
. It is just doing the Try and Catch and displaying an error if it exceeds the 1mb limit I have defined in my web.config.
Is there a syntax or logic error I am not seeing?
Upvotes: 0
Views: 524
Reputation: 6658
Client side Upload Canceling
On modern browsers (FF >= 3.6, Chrome >= 19.0, Opera >= 12.0, and buggy on Safari), you can use the HTML5 File API. When the value of a file input changes, this API will allow you to check whether the file size is within your requirements. Of course, this, as well as MAX_FILE_SIZE, can be tampered with so always use server side validation.
document.forms[0].addEventListener('submit', function( evt ) { var file = document.getElementById('file').files[0]; if(file && file.sizeUpvotes: 1