首页 > 解决方案 > 如何使用 Fancytree 删除文件内容?

问题描述

我正在使用 FancyTree 从用户计算机中拖动文件。在 dragDrop 回调之前一切正常。在这里,我无法检索已删除文件的 blob 数据。许多其他信息可用于data.dataTransfer.files属性,如下所示:

在此处输入图像描述

无论如何,我找不到获取文件内容的方法。我必须将文件发送到像 Uppy File Uploader 这样的上传服务,所以文件数据对我来说是强制性的。

标签: javascriptjqueryfancytree

解决方案


最后我发现我的问题,更笼统地说,是How to get the content from a File Object in Javascript。之后,在这种情况下, FileReader是最好的朋友。

dragDrop: function (node, data) { //dradDrop fancyTree event callback
    ...
    const current_file = data.dataTransfer.files[0]; 
    if (current_file != null) { //i.e. when another fancytree is dropped dataTransfer.files is an empty array
       const reader = new FileReader();
       reader.addEventListener("loadend", function (event) {
          file2upload = event.target.result; //This is the raw content of the file

          //For my purposes the following 3 lines apply a byte array to Blob conversion
          const binaryData = [];
          binaryData.push(file2upload);
          const blob = new Blob([new Uint8Array(file2upload)]);
       });
       reader.readAsArrayBuffer(current_file); //Read the File object as a byte array stream
 }
}

上面的代码段注释很好,所以我想没有什么可添加的。


推荐阅读