gpt4 book ai didi

javascript - 临时上传文件以使用它 - javascript

转载 作者:行者123 更新时间:2023-11-28 14:27:53 24 4
gpt4 key购买 nike

我正在尝试获取通过拖放检索的文件的完整路径。但问题是,出于安全原因,浏览器不允许这样做..:'(

所以我的问题是:

是否可以在拖放文件时,在使用期间将其在线上传,所以暂时(因为我使用 d3.json 来恢复文件,它允许我在线恢复 json 文件..),以及在使用结束时删除在线文件,即关闭选项卡时删除?

再次感谢您的帮助。

最佳答案

您不需要文件的完整路径来读取其内容,而且我确信 d3 允许您直接提供数据,而不是从"file"中读取数据(大概是从 URL 读取)。

只需使用FileReader阅读即可。例如,在 input type="file" 上的 change 处理程序中:

var file = theInput.files[0];
var fr = new FileReader();
fr.onload = function() {
// Use fr.result here, it's a string of the file's contents.
// If it's JSON and you want the data in the form `d3.json` would have
// provided it, do: `var data = JSON.parse(fr.result);`
// and then use `data`.
};
fr.readAsText(file);

实时示例(仅读取文件,不将数据传递给 d3):

document.querySelector("input[type=file]").addEventListener("change", function() {
var file = this.files[0];
var fr = new FileReader();
fr.onload = function() {
console.log("Done");
var pre = document.getElementById("output");
pre.innerHTML = "";
pre.appendChild(
document.createTextNode(fr.result)
);
};
fr.onerror = function() {
console.log("Error reading the file");
};
console.log("Reading...");
fr.readAsText(file);
});
pre {
border: 1px solid black;
padding: 2px;
}
<input type="file">
<hr>
Contents:
<pre id="output"></pre>

关于javascript - 临时上传文件以使用它 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52554884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com