【问题标题】:cordova-plugin-file override window.File科尔多瓦插件文件覆盖 window.File
【发布时间】:2018-04-17 17:42:52
【问题描述】:

当我尝试将文件上传到我的服务器时,当我选择带有<input type="file" /> 的文件时它就像一个魅力,但当我使用cordova-plugin-file 时它发送一个空(0 字节)文件。你猜怎么着?

【问题讨论】:

    标签: cordova cordova-plugins cordova-plugin-file


    【解决方案1】:

    如果您加载cordova-plugin-filenew File 不会创建相同的对象。因为window.Filecordova-plugin-file 覆盖。

    所以我不得不做一个小技巧(感谢https://stackoverflow.com/a/29390393/178575):

    const getFile = dirEntry =>
      new Promise((resolve, reject) => {
        dirEntry.file(file => {
          // window.File is modified by cordova, so we need this trick
          const reader = new FileReader()
          reader.onloadend = function() {
            const blob = new Blob([new Uint8Array(this.result)], {
              type: file.type
            })
            blob.name = file.name
            blob.lastModifiedDate = new Date(file.lastModifiedDate)
            resolve(blob)
          }
          reader.readAsArrayBuffer(file)
        })
    })
    

    【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 2017-08-25
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多