将URL转换为FileReader.readAsDataURL的文件或Blob [英] Convert URL to File or Blob for FileReader.readAsDataURL

查看:1575
本文介绍了将URL转换为FileReader.readAsDataURL的文件或Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考: FileReader.readAsDataURL



考虑下面的例子:

$ $ $ $ $ $ $ $ $ $函数previewFile { b
var reader = new FileReader();

reader.onloadend = function(){
console.log(reader.result);
}
reader.readAsDataURL(file);





$ p


< blockquote>

instanceOfFileReader.readAsDataURL(blob);

blob:从中读取的Blob或文件。





  1. 本地文件URL如何可以:'file:/// C:/ path- to / root.png'
    传递给 readAsDataURL()


  2. 是否可以在Firefox的插件中使用 FileReader()


  3. / Using_the_DOM_File_API_in_chrome_coderel =nofollow noreferrer> File 个例,只需指定一个代码为chrome特权的路径:

     新建文件(/ path / to / file); 



    文件是 Blob ,因此所有的 File 实例也是有效的 Blob s。
    请注意,这需要平台路径,而不是文件网址


  4. 是的, FileReader 可用于插件。


  5. $ b

    File FileReader / code>秒。如果你想在非窗口范围内使用它们(比如 bootstrap.js 或者一个代码模块),你可以使用 nsIDOMFile / nsIDOMFileReader


    Reference: FileReader.readAsDataURL

    Considering the following example:

    function previewFile(file) {
    
      var reader  = new FileReader();
    
      reader.onloadend = function () {
        console.log(reader.result);
      }
      reader.readAsDataURL(file);
    }
    

    It states:

    instanceOfFileReader.readAsDataURL(blob);

    blob: The Blob or File from which to read.

    1. How can a local file URL like: 'file:///C:/path-to/root.png' be passed to the readAsDataURL()

    2. Is FileReader() available in a Firefox Addon?

    解决方案

    1. You can create File instances just by specifying a path when your code is chrome-privileged:

      new File("/path/to/file");
      

      File is a sub-class of Blob, so all File instances are also valid Blobs. Please note that this requires a platform path, and not a file URL.

    2. Yes, FileReader is available to addons.

    File and FileReader are available in all windows. If you want to use them in a non-window scope (like bootstrap.js or a code module), you may use nsIDOMFile/nsIDOMFileReader.

    这篇关于将URL转换为FileReader.readAsDataURL的文件或Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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