javascript - 如何使用 Fancytree 删除文件内容?
问题描述
我正在使用 FancyTree 从用户计算机中拖动文件。在 dragDrop 回调之前一切正常。在这里,我无法检索已删除文件的 blob 数据。许多其他信息可用于data.dataTransfer.files
属性,如下所示:
无论如何,我找不到获取文件内容的方法。我必须将文件发送到像 Uppy File Uploader 这样的上传服务,所以文件数据对我来说是强制性的。
解决方案
最后我发现我的问题,更笼统地说,是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
}
}
上面的代码段注释很好,所以我想没有什么可添加的。
推荐阅读
- sublimetext - 在 SideBarEnhancements 中为打开/运行创建键绑定
- python - Discord Bot 不加入语音频道
- python - Keras plot_model 显示?而不是无
- reactjs - 如何使用 @react-google-maps/api 在 ReactJS 中使用距离矩阵 API?
- json - 将每个 Power Query JSON 数组项用作下一个查询的参数
- json - 如何在 PostgreSQL 函数中优化解析 Json 数组
- python - 用正则表达式交换括号内外的内容
- java - 将 JAR 文件从内部资源复制到临时文件夹
- javascript - Strapi 单元测试
- r - 如何在r中使用条件进行for循环