部分下载带有 Javascript 的文件 [英] partially download a file with Javascript

查看:22
本文介绍了部分下载带有 Javascript 的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们实际上正在使用 javascript 开发远程音乐库管理器,我想知道是否有办法使用 js 下载 MP3 文件的最后 128 字节以获取其 ID3 标签.谢谢.

we're actually working on a Remote Music Library organizer using javascript and I'd like to know if there's a way to download using js the last 128bytes of an MP3 file in order to get its ID3 Tags. thanks.

推荐答案

仅使用 JavaScript 是无法做到的.您将需要服务器上的某些内容(具体取决于您的服务器端技术)来读取文件的相关部分.然后在 JavaScript 中,您可以调用该服务器端部分.

You can't do that with just JavaScript. You'll need something on the server (what exactly depends on your server-side technology) to read the relevant part of the file. Then in JavaScript you can just call that server-side part.

HTTP 协议支持分部分下载文件(就是这个).但是当然 HTTP 对 MP3 文件一无所知,因此您必须知道 JavaScript 中包含 ID3 标签的文件范围.

The HTTP protocol has support for downloading files in parts (which is what the). But of course HTTP knows nothing of MP3 files, so you'll have to know what range of the file contains the ID3 tag in your JavaScript.

这似乎比我最初预期的在纯 JavaScript 中只下载一个 URL 块更可行:

It seems way more feasible than I initially expected to download just a chunk of a URL in pure JavaScript:

xhr = new XMLHttpRequest();
xhr.open("GET", "http://<your domain>/");
xhr.setRequestHeader("Range", "bytes=-256");
xhr.send();

这会请求远程文件的最后 256 个字节.

This requests the last 256 bytes of the remote file.

请注意,您的请求当然受到通常的同源限制,因此您只能使用它从包含您的原始 HTML/JavaScript 的服务器检索文件.

Note that of course your request is subject to the usual same-origin limitations, so you can only use it to retrieve files from the server that contains your original HTML/JavaScript.

这篇关于部分下载带有 Javascript 的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆