4

I'm trying to do some validation of some other fields on the page after the user clicks the upload button of the ajaxfileupload control. OnClientUploadStart is defined to fire before the upload starts. and it works. but I want to cancel the upload if the validation fails. I tried doing "return false;" but that didn't work. How can I cancel the upload?

if the validation fails then I want to cancel

 function uploadStart(sender, args) {

            var FileDescription = document.getElementById("FileDescription").value;

            alert( FileDescription);
         return false;
         }  
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1"
    ThrobberID="myThrobber" OnUploadComplete="AjaxFileUpload1_UploadComplete"
    ContextKeys="" OnClientUploadError="uploadError" OnClientUploadComplete="uploadComplete" OnClientUploadStart="uploadStart" 
    AllowedFileTypes="jpg,jpeg,doc,xls"
    MaximumNumberOfFiles="1"
    runat="server"/>  
Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
user713813
  • 775
  • 1
  • 8
  • 20
  • The sole puspose of this event is to inform client script that server can accept upload and doesn't support upload cancellation. Actually it raised AFTER control started to process files for upload in queue. – Yuriy Rozhovetskiy Aug 13 '13 at 21:45
  • @yuriy-rozhovetskiy yeah, that's part of the problem too, it raised AFTER the control started but the documentation says it's supposed to raise BEFORE! do you know how to start the upload process manually from javascript? That way I can create my own button then check the fields and then do the upload. – user713813 Aug 14 '13 at 01:52
  • @yuriy-rozhovetskiy is there anyway to fire off javascript after the user drops the file? – user713813 Aug 14 '13 at 20:11
  • @yuriy-rozhovetskiy I added the if (Sys.Extended.UI.AjaxFileUpload.prototype._addToQueue) { code as you suggested in the other post. but I'm getting this Error: TypeError: Sys.Extended.UI.AjaxFileUpload.prototype is undefined. any ideas? – user713813 Aug 14 '13 at 23:23

2 Answers2

1

To start uploading manually you can use this script:

function startUpload(){
    $get("<%= AjaxFileUpload1.ClientID %>_UploadOrCancelButton").click();
}
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • thanks! that works. is there anyway to fire off javascript after the user drops the file? – user713813 Aug 14 '13 at 20:10
  • There are two decisions available: http://stackoverflow.com/questions/15678211/ajaxfileupload-automatically-upload-file-once-selected/15678600#15678600 – Yuriy Rozhovetskiy Aug 14 '13 at 20:17
  • I added the if (Sys.Extended.UI.AjaxFileUpload.prototype._addToQueue) { code as you suggested in the other post. but I'm getting this Error: TypeError: Sys.Extended.UI.AjaxFileUpload.prototype is undefined. any ideas? – user713813 Aug 15 '13 at 01:29
1

Cancel download large files (works only for browsers that support HTML5).

    function uploadStartedAjax(sender, args) {
        var maxFileSize = $('#MaxRequestLength').val();
        for (var i = 0; i < sender._filesInQueue.length; i++) {
            var file_size = sender._filesInQueue[i]._fileSize;
            if (file_size > maxFileSize) {
                sender._filesInQueue[i].setStatus("cancelled", Sys.Extended.UI.Resources.AjaxFileUpload_Canceled+' - too large(> ' + maxFileSize + ' byte)!');
                sender._filesInQueue[i]._isUploaded = true;
            } //End if
        } //End for
        return true;
    }
    <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" MaximumNumberOfFiles="200"  Width ="750px" 
                                OnClientUploadStart="uploadStartedAjax" />
AndBag
  • 11
  • 3