简体   繁体   English

使用 javascript 在 Rails 3 中创建一个文件上传

[英]Create a file for upload in rails 3 using javascript

My web app allows users to record their geolocation data.我的 web 应用程序允许用户记录他们的地理位置数据。 I need to somehow get that geolocation data into a file so I can put it in s3 storage.我需要以某种方式将该地理位置数据放入文件中,以便将其放入 s3 存储中。 I have no idea how to go about this, but the controller already has file uploads to s3 set up using paperclip.我不知道如何 go 关于这个,但 controller 已经使用回形针设置了文件上传到 s3。

Is there some way to generate a file with javascript and then attach that file when the user clicks save?有没有办法用 javascript 生成一个文件,然后在用户单击保存时附加该文件? The other option I was thinking is that I could add a bunch of strings to the body using jQuery.data() method, but then I don't know how to attach a string as a file in my rails 3 form.我在想的另一个选择是我可以使用 jQuery.data() 方法向正文添加一堆字符串,但是我不知道如何将字符串作为文件附加到我的 rails 3 表单中。

Any help is appreciated.任何帮助表示赞赏。

maybe you should try out Amazon's simple db instead of s3 for this?也许您应该为此尝试亚马逊的简单数据库而不是 s3? It would be more appropriate than creating files to store data in s3.它比创建文件以将数据存储在 s3 中更合适。

Amazon recently released a ruby sdk for their web services, SDB included: https://github.com/amazonwebservices/aws-sdk-for-ruby Amazon recently released a ruby sdk for their web services, SDB included: https://github.com/amazonwebservices/aws-sdk-for-ruby

Edit: Or better yet, forget using SDB directly.编辑:或者更好的是,忘记直接使用 SDB。 I had forgotten that that SDK includes an implementation of ActiveModel called AWS::Record.我忘记了 SDK 包含一个名为 AWS::Record 的 ActiveModel 实现。 Should make this trivial.应该使这变得微不足道。

I'm assuming you're on Heroku or something and don't have a method of data persistence?我假设您使用的是 Heroku 或其他什么并且没有数据持久性的方法?

edit: looking quickly at paperclip's assign method, there's a chance this would work.编辑:快速查看回形针的分配方法,这有可能会起作用。

yourmodel.your_paperclip_attachment = StringIO.new(params[:your_posted_geolocation_data])

Paperclip appears to handle creating a tempfile from the stream. Paperclip 似乎可以处理从 stream 创建临时文件。

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

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