作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取通过拖放检索的文件的完整路径。但问题是,出于安全原因,浏览器不允许这样做..:'(
所以我的问题是:
是否可以在拖放文件时,在使用期间将其在线上传,所以暂时(因为我使用 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!