简体   繁体   English

如何使用phonegap API将文件传输到Rails 3.1服务器?

[英]How transfer files to a Rails 3.1 server with phonegap API?

I'm using phonegap 1.2.0 for a mobile development that aims to upload a file in a remote server which is a rails3.1 application. 我正在使用phonegap 1.2.0进行移动开发,该开发旨在将文件上传到作为rails3.1应用程序的远程服务器中。

I use the FileTransfer in Phonegap API and I do things exactly like in the Phonegap API example but I cannot get the file in rails. 我在Phonegap API中使用FileTransfer,并且做的事情与Phonegap API示例中的完全相同,但是我无法将文件放在rails中。

Can anybody tell me how get the file in my rails app ? 有人可以告诉我如何在我的Rails应用程序中获取文件吗?

Thank you ! 谢谢 !

I have not done it, but I think you'll have to use the paperclip . 我还没有做过,但是我认为您必须使用回形针

Add this line to your gemfile 将此行添加到您的gemfile

gem "paperclip", "~> 2.4"

Then in your Picture model 然后在您的图片模型中

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.

make a migration 进行迁移

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

end run the migration. 结束迁移。

in you phonegap app you could do. 在您的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>

But there might be a better way to to the form. 但是可能会有更好的方式来生成表格。 Not Tested 未测试

update you can try this as it is. 更新,您可以按原样尝试。 The key is to send in the right options for paperclip. 关键是为回形针发送正确的选项。 Make a rails app with paperclip and find the options paperclip sends to the sever from the form. 使用回形针制作一个Rails应用程序,并找到回形针从表单发送到服务器的选项。 Then add the options to the FileUploadOptions(); 然后将选项添加到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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM