使用PhoneGap FileTransfer类将图像上载到基于RoR的服务器时出错

时间:2013-07-04 08:57:37

标签: ruby-on-rails cordova

my_server_url我正在使用Cordova for iOS并尝试使用以下代码将图像上传到基于RoR的服务器:

var options = new FileUploadOptions();
var imagefileName = "capture.jpg";

options.fileKey = "capture";
options.fileName = "file://" + capturePath;
options.chunkedMode = false;

$('#capturedImagePreview').attr('src', options.fileName);

options.params = 
{
    some_id: 10,
};

alert("Uploading file " + options.fileName);

var uploader = new FileTransfer();

uploader.upload(capturePath, "https://my_server_url/api/v1/uploadCapture",
    function(event)
    {
        app.hideLoading();
        alert("Uploading succeeded: " + JSON.stringify(event));
    },

    function (event)
    {
        app.hideLoading();
        alert("Uploading failed: " + JSON.stringify(event));
    },

    options);

文件名似乎是正确的(如file:///var/mobile/Applications/{SOME_KIND_OF_UID}/tmp/photo.jpg);图像本身显示在手机图库中。在服务器日志中出错:

Parameters:
{
    "capture"=>#<ActionDispatch::Http::UploadedFile:0x00000006d1efd8 @original_filename="capture.jpg", @content_type=nil, @headers="Content-Disposition: form-data; name=\"capture\"; filename=\"capture.jpg\"\r\nContent-Length: 322009\r\n", @tempfile=#<File:/tmp/RackMultipart20130704-26512-1ja0glt>>
}

获取服务器响应:

75 7: unexpected token at `Syntax: exiftool [OPTIONS] FILE\\n\\nConsult the exiftool documentation for a full list of options

此外,在此回复中

bytesSent: 352039, responseCode: 200

我对这些消息感到困惑,我不知道Ruby平台。请求帮助。处理我上传的服务器代码部分如下所示。

begin

  Rails.logger.debug"============uploaded file : #{params[:capture]} ==========================="
  src = process_upload(params[:capture]) if params[:capture].present?
  Rails.logger.debug "============== capture SRC :  #{src} =============================="

  # media metatags
  meta = %x(exiftool -u -d "%Y-%m-%d %H:%M:%S" -json #{src})
  Rails.logger.debug "========== Mobile Meta information : #{meta} ============="
  metatag = JSON.parse(meta)[0]

  capture = Capture.new(:media => File.open(src))

  # ....

服务器日志

 Processing by Api::V1::DataController
  Parameters: {"capture"=>#<ActionDispatch::Http::UploadedFile:0x000000076f1bf0 @original_filename="photo_002.jpg", @content_type=nil, @headers="Content-Disposition: form-data; name=\"capture\"; filename=\"file:///var/mobile/Applications/F2297592-F606-40E5-A832-3CBD7EC8C1CC/tmp/photo_002.jpg\"\r\nContent-Length: 275115\r\n", @tempfile=#<File:/tmp/RackMultipart20130704-26512-1lm4o71>>}
WARNING: Can't verify CSRF token authenticity

============uploaded file : #<ActionDispatch::Http::UploadedFile:0x000000076f1bf0> ===========================

================== Upload Process Error: can't convert ActionDispatch::Http::UploadedFile into String ================
============== capture SRC :   ==============================
========== Mobile Meta information : Syntax:  exiftool [OPTIONS] FILE

Consult the exiftool documentation for a full list of options.
 =============
================== Mobile upload Error: 757: unexpected token at 'Syntax:  exiftool [OPTIONS] FILE

Consult the exiftool documentation for a full list of options.
' ================

1 个答案:

答案 0 :(得分:0)

问题解决了。服务器代码中存在逻辑错误。通过重写它来修复

def capture_upload
    current_device_user
begin

    src = saveTempFileToStorage(:tempfilePath => params[:capture].tempfile.path)

rescue Exception => e
    render :json => {:status => "failed",:content => "#{e.message}"}
end

end



def saveTempFileToStorage(tempfilePath)
begin

    target_file_full_path = "/home/deployer/mobileuploads/image01.jpg"
    FileUtils.cp(tempfilePath, target_file_full_path)

    return target_file_full_path

rescue Exception => e
    Rails.logger.debug "saveTempFileToStorage error: #{e.message}"
    return
end  

end