4

I am working on some legacy code which is using Asp.net and ajax where we do one functionality to upload a pdf. To upload file our legacy code uses AjaxUpload, but I observed some weird behavior of AjaxUpload where onComplete event is getting called before actual file got uploaded by server side code because of this though the file got uploaded successfully still user gets an error message on screen saying upload failed.

And here the most weird thins is that same code was working fine till last week.

Code:

initFileUpload: function () {
      debugger;
  new AjaxUpload('aj-assetfile', {
    action: '/Util/FileUploadHandler.ashx?type=asset&signup=False&oldfile=' + assetObj.AssetPath + '&as=' + assetObj.AssetID,
    //action: ML.Assets.handlerPath + '?action=uploadfile',
    name: 'AccountSignupUploadContent',
    onSubmit: function (file, ext) {
      ML.Assets.isUploading = true;

      ML.Assets.toggleAsfMask(true);

      // change button text, when user selects file         
      $asffile.val('Uploading');
      $astfileerror.hide();

      // If you want to allow uploading only 1 file at time,
      // you can disable upload button
      this.disable();

      // Uploding -> Uploading. -> Uploading...
      ML.Assets.interval = window.setInterval(function () {
        var text = $asffile.val();
        if (text.length < 13) {
          $asffile.val(text + '.');
        } else {
          $asffile.val('Uploading');
        }
      }, 200);

      //if url field block is visible 
      if ($asseturlbkl.is(':visible')) {
        $asfurl.val(''); //reset values of url
        $asfurl.removeClass('requiref error'); //remove require field class              
        $asfurlerror.hide(); //hide errors
      }
    },
      onComplete: function (file, responseJSON) {
          debugger;
      ML.Assets.toggleAsfMask(false);
      ML.Assets.isUploading = false;
      window.clearInterval(ML.Assets.interval);
      this.enable();

      var success = false;
      var responseMsg = '';
      try {
        var response = JSON.parse(responseJSON);
        if (response.status == 'success') { //(response.getElementsByTagName('status')[0].textContent == 'success') {            
          success = true;
        } else {
          success = false;
          responseMsg = ': ' + response.message;
        }
      } catch (e) {
        success = false;
      }

      if (success) {
        assetObj.AssetMimeType = response.mimetype;
        $asffile.val(response.path);
        $asffile.valid(); //clear errors
        ML.Assets.madeChanges();

        if (ML.Assets.saveAfterUpload) { //if user submitted form while uploading              
          ML.Assets.saveAsset(); //run the save callback              
        }
      } else { //error
        assetObj.AssetMimeType = "";
        $asffile.val('');
        $astfileerror.show().text('Upload failed' + responseMsg);
        //if url field block is visible  and type is not free offer.  
        if ($asseturlbkl.is(':visible') && this.type !== undefined && assetObj.AssetType != this.type.FREEOFFER) {
          $asfurl.addClass('requiref'); //remove require field class
        }
        ML.Assets.hideLoader();
      }
    }
  });
}
Dreamer
  • 586
  • 3
  • 7
  • 23
  • I got the reason, it happening because of chrome update. Same code is working fine with old version of chrome. – Dreamer May 29 '20 at 07:56

1 Answers1

2

I was facing the same issue but I fixed it with some minor change in plugin.

When “iframeSrc” is set to “javascript:false” on https or http pages, Chrome now seems to cancel the request. Changing this to “about:blank” seems to resolve the issue.

Old Code:

var iframe = toElement('<iframe src="javascript:false;" name="' + id + '" />');

New Code with chagnes:

var iframe = toElement('<iframe src="about:blank;" name="' + id + '" />');

After changing the code it's working fine. I hope it will work for you as well. :)

Reference (For more details): https://www.infomazeelite.com/ajax-file-upload-is-not-working-in-the-latest-chrome-version-83-0-4103-61-official-build-64-bit/

Manjunath Bilwar
  • 2,215
  • 19
  • 16