【问题标题】:How transfer files to a Rails 3.1 server with phonegap API?如何使用 phonegap API 将文件传输到 Rails 3.1 服务器?
【发布时间】:2011-12-12 08:05:39
【问题描述】:

我正在将 phonegap 1.2.0 用于移动开发,旨在将文件上传到远程服务器中,该服务器是一个 rails3.1 应用程序。

我在 Phonegap API 中使用 FileTransfer,我做的事情与 Phonegap API example 中的完全一样 但我无法在 rails 中获取文件。

谁能告诉我如何在我的 Rails 应用程序中获取文件?

谢谢!

【问题讨论】:

  • 您在 te rails 服务器上收到任何错误消息吗?
  • 没有错误。我创建了一个具有属性名称的模型图片:字符串。调用了 post 方法 CREATE 但创建的模型为空。我不知道如何告诉 rails 获取文件名放入选项和给定图像。选项在标题中,但不在参数中.....

标签: ruby-on-rails mobile cordova file-transfer rails-3.1


【解决方案1】:

我还没有这样做,但我认为你必须使用paperclip

将此行添加到您的 gemfile 中

gem "paperclip", "~> 2.4"

然后在你的图片模型中

has_attached_file :image, :styles => { :medium => "150x150>", :thumb => "50x50#" }
 # you don't need the styles, I just put them there so you know you can.

进行迁移

class AddImageToPicture < ActiveRecord::Migration
  def self.up
      add_column :picture, :image_file_name, :string 
      add_column :picture, :image_content_type, :string
      add_column :picture, :image_file_size, :integer 
  end

结束 运行迁移。

在你的 phonegap 应用程序中你可以做到。

<form accept-charset="UTF-8" action="/pictures" class="picture_user" enctype="multipart/form-data" id="new_user" method="post">
<input id="user_image" name="user[image]" type="file">
</form>

但可能有更好的方法来获取表单。未测试

更新 你可以试试这个。关键是为回形针发送正确的选项。使用回形针制作一个 rails 应用程序,并从表单中找到回形针发送到服务器的选项。然后将选项添加到 FileUploadOptions();

var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";
            options.params = params;
            var ft = new FileTransfer();
            ft.upload(imageURI, "http://some.server.com/upload.php", win, fail, options);

【讨论】:

  • 我无法使用表单。看一下phonegap FileTransfer 方法。 codevar ft = new FileTransfer(); ft.upload(fileURI, "some.server.com/upload.php", 胜利, 失败, 选项);code.也许回形针是一个很好的解决方案,但我仍然不知道如何从 rails 的标题中获取文件名和文件
  • 我对 aswer 进行了更新。从添加回形针开始,然后找到正确的选项
  • 如果可行,请接受我的回答。look here
  • 如果它有效,我会 ;) 但是我在 rails doc 中看到我可以访问 request.headers ,它是一个散列。我认为它将包含 FileUploadOptions 之类的文件名....
  • 这是我在 Rails 应用程序上遇到的错误:[2012-05-15 22:36:31] ERROR ArgumentError:negative length -9 given /Users/luciano/.rvm/gems /ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/multipart/parser.rb:107:in read' /Users/luciano/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/multipart/parser.rb:107:in get_current_head_and_filename_and_content_type_and_name_and_body' /Users/luciano/.rvm/gems/ruby-1.9.3 -p0/gems/rack-1.4.1/lib/rack/multipart/parser.rb:19:in block in parse' /Users/luciano/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.1/lib/rack/multipart/parser.rb:17:in loop'...
猜你喜欢
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 2017-04-20
  • 2017-01-09
  • 2020-03-04
相关资源
最近更新 更多