最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Permission denied for clicking link in Internet Explorer 11 - Stack Overflow

programmeradmin7浏览0评论

I modified an existing AngularJS-App, which lists customers, by adding a button, that allows to download the customer information as a vcard. I create the vcard in Javascript directly on click. The download-button calls the following function on click with the customer-item as argument:

function transcodeToAnsi(content){
  var encoding = "windows-1252";
  var nonstandard = {NONSTANDARD_allowLegacyEncoding: true};
  return new TextEncoder(encoding, nonstandard).encode(content);
}

$scope.download = function(item) {
  var filename = 'contact.vcf';
  var aId = "vcard";

  var content = createVCard(item);
  var encoded = transcodeToAnsi(content);

  var blob = new Blob([ encoded ], { type : 'vcf' });
  var url = (window.URL || window.webkitURL).createObjectURL(blob);

  $("body").append('<a id="' + aId + '" href="' + url + '" download=' + filename + ' class="hidden"></a>');
  $timeout(function(){
    document.getElementById(aId).click();
    $("#" + aId).remove();
  })

  return false;
}

In the createVCard-function I just create the file content as a String, so it should not enter into the problem. The transcoding is done by this library:

The function works without problem in Firefox and Chrome, but not in IE11. The following error is given in the console:

Error: Permission denied
at Anonymous function (http://***/Contacts2015/js/contacts.js:169:9)
at Anonymous function (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:14305:11)
at pleteOutstandingRequest (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:4397:7)
at Anonymous function (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:4705:7) undefined

Line 169 is this instruction of the function above:

document.getElementById(aId).click();

The same error is displayed, when this statement is input in the console manually.

I would appreciate every hint about the reason and even more a good workaround.

EDIT

Corrected error-line and typo.

I modified an existing AngularJS-App, which lists customers, by adding a button, that allows to download the customer information as a vcard. I create the vcard in Javascript directly on click. The download-button calls the following function on click with the customer-item as argument:

function transcodeToAnsi(content){
  var encoding = "windows-1252";
  var nonstandard = {NONSTANDARD_allowLegacyEncoding: true};
  return new TextEncoder(encoding, nonstandard).encode(content);
}

$scope.download = function(item) {
  var filename = 'contact.vcf';
  var aId = "vcard";

  var content = createVCard(item);
  var encoded = transcodeToAnsi(content);

  var blob = new Blob([ encoded ], { type : 'vcf' });
  var url = (window.URL || window.webkitURL).createObjectURL(blob);

  $("body").append('<a id="' + aId + '" href="' + url + '" download=' + filename + ' class="hidden"></a>');
  $timeout(function(){
    document.getElementById(aId).click();
    $("#" + aId).remove();
  })

  return false;
}

In the createVCard-function I just create the file content as a String, so it should not enter into the problem. The transcoding is done by this library: https://github./inexorabletash/text-encoding

The function works without problem in Firefox and Chrome, but not in IE11. The following error is given in the console:

Error: Permission denied
at Anonymous function (http://***/Contacts2015/js/contacts.js:169:9)
at Anonymous function (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:14305:11)
at pleteOutstandingRequest (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:4397:7)
at Anonymous function (http://***/static/9bojdXAkdR8XdVMdSTxZAgzEwGWhHMwgpuONdU2Y8F4.js:4705:7) undefined

Line 169 is this instruction of the function above:

document.getElementById(aId).click();

The same error is displayed, when this statement is input in the console manually.

I would appreciate every hint about the reason and even more a good workaround.

EDIT

Corrected error-line and typo.

Share Improve this question edited May 30, 2016 at 9:32 Nils-o-mat asked May 30, 2016 at 9:20 Nils-o-matNils-o-mat 1,2571 gold badge19 silver badges34 bronze badges 4
  • This might be a problem because you appended the aId link to the DOM - it could be that it isn't sure how to remove it again. I'd suggest taking a look at removing DOM appended elements, perhaps that would help. – Forest Commented May 30, 2016 at 9:26
  • @Furze Sorry, my mistake. I printed the wrong line... I changed it. – Nils-o-mat Commented May 30, 2016 at 9:27
  • Try changing document.getElementById(aId).click(); to $("#" + aId).trigger("click");. – Forest Commented May 30, 2016 at 9:35
  • @Furze Sadly this gets me the same error... – Nils-o-mat Commented May 30, 2016 at 9:41
Add a ment  | 

1 Answer 1

Reset to default 21

You cannot directly open blobs in Microsoft IE. You must use window.navigator.msSaveOrOpenBlob. There's also msSaveBlob if that's what you need.

$scope.download = function() {
    //etc... logic...
    var blob = new Blob([encoded], {type: 'vcf'});

    //for microsoft IE
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else { //other browsers
        var a = document.createElement('a');
        a.style = "display:none";
        a.href = URL.createObjectURL(blob);
        a.download = "filename.jpg";
        a.click();
    }
}

One last thing: the previous code won't work on firefox because firefox doesn't support click(). You can prototype its behavior using this snippet:

HTMLElement.prototype.click = function() {
   var evt = this.ownerDocument.createEvent('MouseEvents');
   evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   this.dispatchEvent(evt);
}
发布评论

评论列表(0)

  1. 暂无评论