阅读内容blob:http://javascript
Reading contents of blob:http:// javascript
这可能是一个相对简单的答案,我可能只是严重忽略了它。
我使用 API 打包我们的一些数据并将其发送回给我。它提供了一个 url 例如,blob:http://localhost:3001/somenumber.
我假设这个 url 是我的文件所在的位置?当我在浏览器中手动点击 url 时,它会下载我需要的文件(只是一个基本数据对象)。但是我似乎无法真正让这个文件输出数据,所以我可以在我的客户端 code/javascript 中处理它。我玩弄了 FileReader 系统,但没有成功。
任何帮助都会很棒。基本上,我想获取数据库服务提供给我的这个 blob url,并读取其内容,以便将数据输出到视图。
谢谢!
你可以这样读取数据:
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:3001/somenumber", true);
req.responseType = "arraybuffer";
req.onreadystatechange = function(event) {
if (req.readyState == 4 && req.status == 200) {
var blob = new Blob([req.response], {type: "application/pdf"}); // or "image/png", or others...
var fileURL = URL.createObjectURL(blob); // Create a temp ULR to the data (pdf, image...)
location.href = fileURL; // switch location to this new URL
}
};
你可以看看https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data。
这可能是一个相对简单的答案,我可能只是严重忽略了它。
我使用 API 打包我们的一些数据并将其发送回给我。它提供了一个 url 例如,blob:http://localhost:3001/somenumber.
我假设这个 url 是我的文件所在的位置?当我在浏览器中手动点击 url 时,它会下载我需要的文件(只是一个基本数据对象)。但是我似乎无法真正让这个文件输出数据,所以我可以在我的客户端 code/javascript 中处理它。我玩弄了 FileReader 系统,但没有成功。
任何帮助都会很棒。基本上,我想获取数据库服务提供给我的这个 blob url,并读取其内容,以便将数据输出到视图。
谢谢!
你可以这样读取数据:
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:3001/somenumber", true);
req.responseType = "arraybuffer";
req.onreadystatechange = function(event) {
if (req.readyState == 4 && req.status == 200) {
var blob = new Blob([req.response], {type: "application/pdf"}); // or "image/png", or others...
var fileURL = URL.createObjectURL(blob); // Create a temp ULR to the data (pdf, image...)
location.href = fileURL; // switch location to this new URL
}
};
你可以看看https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data。