Is there any way to specify a suggested filename when using data: URI?

前端 未结 16 1820
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:09

If for example you follow the link:

data:application/octet-stream;base64,SGVsbG8=

The browser will prompt you to download a file consisting of t

16条回答
  •  一整个雨季
    2020-11-22 03:44

    var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
    var sessionId ='\n';
    var token = '\n';
    var caseId = CaseIDNumber + '\n';
    var url = casewebUrl+'\n';
    var uri = sessionId + token + caseId + url;//data in file
    var fileName = "file.i4cvf";// any file name with any extension
    if (isIE)
        {
                var fileData = ['\ufeff' + uri];
                var blobObject = new Blob(fileData);
                window.navigator.msSaveOrOpenBlob(blobObject, fileName);
        }
        else //chrome
        {
            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
             window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (fs) {
                fs.root.getFile(fileName, { create: true }, function (fileEntry) { 
                    fileEntry.createWriter(function (fileWriter) {
                        var fileData = ['\ufeff' + uri];
                        var blob = new Blob(fileData);
                        fileWriter.addEventListener("writeend", function () {
                            var fileUrl = fileEntry.toURL();
                            var link = document.createElement('a');
                            link.href = fileUrl;
                            link.download = fileName;
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        }, false);
                        fileWriter.write(blob);
                    }, function () { });
                }, function () { });
             }, function () { });
        }
    

提交回复
热议问题