gpt4 book ai didi

javascript - 如何使用 JavaScript 按 block 读取任何本地文件?

转载 作者:行者123 更新时间:2023-11-28 10:42:05 26 4
gpt4 key购买 nike

如何在本地按 block (2kb或更多)读取任何大文件(大于1 GB),然后将 block 转换为字符串,处理字符串,然后获取下一个 block ,依此类推,直到结束文件?

我只能读取小文件并将其转换为字符串,正如您从代码中看到的那样,我不知道如何按 block 读取文件。如果我尝试使用大于 10mb 的文件,浏览器就会卡住。

<html>
<head>
<title>Read File</title>
</head>

<body>
<input type="file" id="myFile">
<hr>
<textarea style="width:500px;height: 400px" id="output"></textarea>

<script>
var input = document.getElementById("myFile");
var output = document.getElementById("output");
input.addEventListener("change", function () {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', function (e) {
output.textContent = e.target.result;
});
reader.readAsBinaryString(myFile);
}
});
</script>

</body>
</html>

以下是我在研究如何实现它时在 StackOverflow 上找到的链接和答案,但它没有解决我的问题。

1: This question was asking about how to do it using UniversalXPConnect, and only in Firefox, which is why i found the answer there to be irrelevant, because I use Chrome and don't know what UniversalXPConnect is. How to read a local file by chunks in JavaScript

2: This question was asking about how to read text files only, but I want to be able to read any file not just text, and also by chunks, which makes the answers there irrelevant, but i liked how short the code of the answer was. Reading local text file into a JavaScript array [duplicate]

3: This also is about text files and doesn't show how to read files by chunks How to read a local text file.

我懂一点Java,你可以轻松地做到这一点;

char[] myBuffer = new char[512];
int bytesRead = 0;
BufferedReader in = new BufferedReader(new FileReader("foo.mp4"));
while ((bytesRead = in.read(myBuffer,0,512)) != -1){
...
}

但我是 JavaScript 新手

最佳答案

我能够通过指定切片开始位置和 block 结束位置的属性来切片文件来解决这个问题,然后我将其包含在一个 while 循环中,以便每个循环 block 位置将根据到所需的 block 大小,直到文件末尾。

但是运行之后,我最终得到了文本区域中 block 的最后一个值,因此为了显示所有二进制字符串,我在每次迭代时连接输出。

<html>
<head>
<title>Read File</title>
</head>

<body>
<input type="file" id="myFile">
<hr>
<textarea style="width:500px;height: 400px" id="output"></textarea>

<script>
var input = document.getElementById("myFile");
var output = document.getElementById("output");
var chunk_size = 2048;
var offset = 0;
input.addEventListener("change", function () {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var size = myFile.size; //getting the file size so that we can use it for loop statement
var i=0;
while( i<size){
var blob = myFile.slice(offset, offset + chunk_size); //slice the file by specifying the index(chunk size)
var reader = new FileReader();
reader.addEventListener('load', function (e) {
output.textContent += e.target.result; //concatenate the output on each iteration.
});
reader.readAsBinaryString(blob);
offset += chunk_size; // Increment the index position(chunk)
i += chunk_size; // Keeping track of when to exit, by incrementing till we reach file size(end of file).
}
}
});
</script>

</body>
</html>

关于javascript - 如何使用 JavaScript 按 block 读取任何本地文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50254537/

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