0

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

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

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

谢谢 !

4

1 回答 1

1

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

将此行添加到您的 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);
于 2011-12-12T09:54:55.787 回答