【问题标题】:phonegap filetransfer.upload to send image to server 200 bytes too shortphonegap filetransfer.upload 将图像发送到服务器 200 字节太短
【发布时间】:2013-01-16 01:00:49
【问题描述】:

我正在尝试使用 phonegap 的 filetransfer.upload 发送图像文件,但返回的文件已损坏,查看 logcat 发送的文件似乎太短了 200 字节。

这是我发送文件的代码

sendImageFile = function (imageURI, imageName) {
    writelog("Sending image file", 1);
    var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            var params = new Object();
            params.value1 = "image name";
            options.params = params;
            options.chunkedMode = false;
            var ft = new FileTransfer();
            writelog("image uri length " + imageURI.length, 1);
    writelog("Image options set up successfully", 1);
    var ft = new FileTransfer();
    ft.upload(imageURI, uploadurl, win, transFail, options);
}

这里有一些来自 logcat 的相关行

01-07 12:27:30.743: D/FileTransfer(20066): Uploaded 114688 of 145432    bytes 

01-07 12:27:31.571: D/FileTransfer(20066): got response from    server 

01-07 12:27:31.696: D/CordovaLog(20066): Code = 200 

01-07 12:27:31.696: D/CordovaLog(20066): Response = 12099

01-07 12:27:31.696: D/CordovaLog(20066): Sent = 145236

任何帮助将不胜感激。

谢谢

马特

【问题讨论】:

    标签: android cordova file-transfer


    【解决方案1】:

    找到解决方案。我的服务器接受作为文件发送的所有数据,而不是从表单提交中分解它(我相信)。这导致在主图像数据之前有几行文本。

    为了解决这个问题,我安装了一个旧的 fileTransfer 插件 (https://github.com/phonegap/phonegap-plugins/tree/master/Android/FileUploader),恢复到 phonegap 的 1.8.1 版(因为我不确定目前如何更新旧插件)。

    编辑了 fileUpload.java 文件以删除要发送的文件之前的所有文本。现在它可以被服务器读取了

    【讨论】:

      【解决方案2】:

      您不能在 FileTransfer 上传方法中使用从 Android 上的相机成功回调获得的 imageUri,您必须首先将 uri 解析为这样的文件名:

      navigator.camera.getPicture(function(imageURI){
      
          window.resolveLocalFileSystemURI(imageURI, function(fileEntry) {
              fileEntry.file(function(fileObj) {
      
                  var fileName = fileObj.fullPath;
      
                  //now use the fileName in your upload method
                  var options = new FileUploadOptions();
                  options.fileKey = "file";
                  options.fileName = fileName.substr(fileName.lastIndexOf('/')+1);
                  //...
              });
          });
      
      }, errorFn, cameraParams);
      

      【讨论】:

      • 感谢您的帮助。它现在正在使用 fileEntry.file。查看通信数据,遇到正确的字节数,但图像仍然损坏。它在设备上很好,因为它会在拍摄图像后显示在页面上
      • 也许你的服务器端代码有问题。可以肯定的是,在 fileObj.fullPath 上使用 console.log 看看你得到了什么。如果您看到正确的文件名(带有扩展名),则您的客户端代码应该没问题,因此您的服务器端可能会出错。
      • @user1487927 FileTransfer 代码实际上并非如此。我修复了它,以便它可以解析内容:在 Cordova 版本之一中的文件路径的 uri。
      • 服务器所做的只是将接收到的文件放在一个 sql 表中(未编辑)。上传路径正确,文件名也正确,都有扩展名。发送的字节数仍然不等于内容长度(我上次尝试时为 181220 与 181024)。有没有办法在标头中发送长度(这会影响什么吗?)
      • 我会尝试将文件保存在硬盘上而不是 sql 表上。如果您可以从硬盘打开文件,则说明数据库有问题。
      猜你喜欢
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2018-05-04
      • 2018-09-21
      相关资源
      最近更新 更多