部分用Javascript下载文件 [英] partially download a file with Javascript

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

问题描述

实际上,我们正在使用javascript处理远程音乐库组织者,我想知道是否有方法使用js下载MP3文件的最后128bytes以获取其ID3标签。谢谢。

解决方案

只有JavaScript才能做到这一点。您需要在服务器上安装某些东西(具体取决于您的服务器端技术)以读取文件的相关部分。然后在JavaScript中,您可以调用该服务器端部分。

Update 1



HTTP协议支持用于下载部分文件(这是什么)。但是,HTTP当然不知道MP3文件,所以你必须知道你的JavaScript中ID3标签的范围是什么。



更新2



看起来比我最初预期的在纯JavaScript中下载一大段URL的方式更可行:

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

这要求远程文件的最后256个字节。



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


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.

解决方案

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.

Update 1

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.

Update 2

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();

This requests the last 256 bytes of the remote file.

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天全站免登陆