首页 > 解决方案 > Node.js:可以将 BLOB 转换为文件并使用 shell 使用默认应用程序打开它吗?

问题描述

使用shell,我们可以通过其默认应用程序打开文件。但是有可能将一个blob转换成文件吗?

var fileurl = '';	//url of a file, example is docx file.
var filename = $(this).text().replace(/^\s+/g, '');

$.ajax({
        url: fileurl,
        method: 'GET',
        xhrFields: {
            responseType: 'blob'
        },
        success: function (data) {
          //convert the blob into file
          var file = new File([data], filename, {type: data.type, lastModified: Date.now()});
          const {shell} = require('electron');
          shell.openItem(file); //open the converted file with it's default application
        }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我试图将 blob 转换为文件,认为它可能会起作用,但它失败并且错误提示Error processing argument at index 0, conversion failure from #<File>

标签: javascriptjquerynode.jselectron

解决方案


Electronshell.openItem(filepath:string)明确要求现有本地文件的路径。https://electronjs.org/docs/api/shell#shellopenitemfullpath

除非将文件保存到某个临时位置,否则不能直接传递 blob。


推荐阅读