Tom
Tom

Reputation: 1628

Stopping a file upload

I have this form submission:

<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >

I'm trying to get the file size before upload and if it's too large stop the submission.

iSize is reporting the correct file size, the alert is being shown, but the submission continues.

How do I exit the upload once the alert is shown ?

function startUpload(){
    var iSize = ($("#myfile")[0].files[0].size / 1024);
    console.log(iSize)
    if (iSize > 10) {
        alert('file size to big');
        return;
    }


      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}

Upvotes: 2

Views: 483

Answers (1)

Lain
Lain

Reputation: 3726

You have to pass on the return value of the function:

<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startUpload();' >

Furthermore on a sidenote, the success part of the function stopUpload() makes less sense if you consider a form.submit() happening in the process. One would have to upload it by sending it to an iframe or by using ajax to make proper use of it.

Upvotes: 1

Related Questions