简体   繁体   English

使用HTML5 File API读取存储在某个URL上的文件

[英]Read File stored on some URL using HTML5 File API

I'm using HTML5 file API to upload files to my web application. 我正在使用HTML5文件API将文件上传到我的Web应用程序。

I have an input element on my web page using which I read files and call upload function 我的网页上有一个输入元素,我用它来读取文件和调用上传功能

<input type="file">

$('input[type="file"]').on("change",function(e){
      console.log(this.files);
      // upload each file in this.files
});

This works perfectly for native files on os. 这适用于os上的本机文件。 I want to now upload remote files eg, example.com/blah/file1.jpg My question is how do I read this file using File API? 我想现在上传远程文件,例如example.com/blah/file1.jpg我的问题是如何使用File API读取此文件? Is there a way to do it? 有办法吗?

You can download remote files over XMLHttpRequest , and process them as Blob . 您可以通过XMLHttpRequest下载远程文件,并将它们作为Blob Then upload it to another server. 然后将其上传到另一台服务器。 The upload has to be over XMLHttpRequest . 上传必须通过XMLHttpRequest It relies on the browser's implementation of XHR Level 2. This link contains the code snippets you will need: 它依赖于浏览器的XHR Level 2实现。此链接包含您需要的代码片段:

http://www.html5rocks.com/en/tutorials/file/xhr2/ http://www.html5rocks.com/en/tutorials/file/xhr2/

It has both snippets for downloading remote file as a Blob and uploading a Blob to a server. 它具有下载远程文件作为两个片段Blob和上传Blob到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM