【问题标题】:PhoneGap FileTransfer Upload Not Found未找到 PhoneGap FileTransfer 上传
【发布时间】:2013-01-28 22:48:16
【问题描述】:

我正在尝试通过 FileTransfer.upload 方法上传使用设备相机拍摄的图像,并且一直得到FILE_NOT_FOUND_ERR。我正在通过 PhoneGap Build 使用 Cordova 2.2.0。

我正在拍照并将其 URI 保存到 localStorage(通过 Backbone):

navigator.camera.getPicture(
    (file_uri) =>
        params.file_uri = file_uri
        params.is_image = 1
        collection.create params
    (message) ->
        U.Alert "Failed: #{message}"
    options
)

然后尝试上传该图像:

file_uri = @get('file_uri')

transferOpts = new FileUploadOptions
transferOpts.fileKey = 'attachment'
transferOpts.fileName = file_uri.substr file_uri.lastIndexOf('/') + 1
transferOpts.chunkedMode = false

transfer = new FileTransfer
transfer.upload(file_uri, @url(), success, error, transferOpts)

这样,FileTransfer 的 error 回调总是会抛出以下内容:

{
    code: 1,
    http_status: 411,
    source: "content://media/external/images/media/8416",
    target: "https://mywebapp.com/api/endpoint"
}

代码 1 是 PhoneGap 的 FILE_NOT_FOUND_ERR 响应代码。但我已经验证该文件存在于设备上。首先,我可以将file_uri 显示为屏幕上的图像。其次,我可以通过 Weinre 调试器检索图像文件的内容,如下所示:

var reader = new FileReader();
reader.onloadend = function(e) {
    console.log('Read as data URL');
    console.log(e.target.result);
};
reader.readAsDataUrl(file_uri);

记录的data:image/jpeg;base64 响应是正确的图像。

现在,我什至尝试像这样手动解析 URI,正如我在 StackOverflow 的一个问题中看到的那样:

resolveLocalFileSystemURI file_uri, (fileEntry) => fileEntry.file (fileObj) =>
    transferOpts = new FileUploadOptions
    transferOpts.fileKey = 'attachment'
    transferOpts.fileName = fileObj.name
    transferOpts.chunkedMode = false

    transfer = new FileTransfer
    transfer.upload(fileObj.fullPath, @url(), success, error, transferOpts)

在这种情况下,fileObj.fullPath 解析为:/mnt/sdcard/Android/data/com.COMPANY.APPNAME/cache/1359412873161.jpg

而且仍然没有运气。在这一点上,我确定我错过了一些愚蠢的东西!

【问题讨论】:

    标签: cordova


    【解决方案1】:

    我遇到了类似的问题,并通过使用格式为“file://”的源 uri 解决了

    【讨论】:

    • 我已经有了标题“file://”(自动添加)但我有同样的错误
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多