0
 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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Christian4423
  • 1,746
  • 2
  • 15
  • 25
  • The error is telling you the problem. You got to go into your Web.Config file and increase your `MaxRequestLength`. Look at [this link](http://www.codeproject.com/Articles/10842/Setting-up-Web-config-to-allow-uploading-of-large). – Icemanind May 22 '15 at 18:26
  • I do not want to increase the file size. I want to inform the user that the file they have choose to upload is too big, and that they should upload a smaller file. – Christian4423 May 22 '15 at 18:32
  • fileSize is used in the ELSE statement without being initialized. You should indent your code properly and you will see how strange your if statement is. – the_lotus May 22 '15 at 18:43
  • 1
    Yikes. I noticed that before I submit this code. I was copy and pasting around and put in the wrong spot. Must have not copied my clipboard. Let me edit it. – Christian4423 May 22 '15 at 18:48
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 22 '15 at 18:53
  • Okay I will do better next time, thank you. – Christian4423 May 22 '15 at 18:58

1 Answers1

1

Check file size before upload

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.size
Community
  • 1
  • 1
JJS
  • 6,431
  • 1
  • 54
  • 70
  • I like it. Unfortunately, with the holiday most my bosses went on a 4 day weekend so I will not know if they would want to implement this idea just yet. I am not sure if they would want it to be client side due to security. But I will mention it. – Christian4423 May 22 '15 at 19:01
  • regardless of whether your boss implements, if the answer is valid, please upvote or mark as correct – JJS May 22 '15 at 20:12