使用 Carrierwave 将原始文件上传到 Rails
我的客户正在尝试从黑莓和 Android 手机上传图像。他们不喜欢发布 a) 表单参数或 b) 多部分消息。他们想要做的是仅使用文件中的数据对 url 进行 POST。
类似这样的事情可以在curl中完成: curl -d @google.png http://server/postcards/1/photo.json -X POST
我希望将上传的照片放入明信片模型的照片属性中并放入正确的目录。
我正在控制器中执行类似的操作,但目录中的图像已损坏。我现在正在手动将文件重命名为“png”:
def PostcardsController < ApplicationController
...
# Other RESTful methods
...
def photo
@postcard = Postcard.find(params[:id])
@postcard.photo = request.body
@postcard.save
end
模型:
class Postcard < ActiveRecord::Base
mount_uploader :photo, PhotoUploader
end
My clients are trying to upload an image from Blackberry and Android phones. They don't like posting a)form parameters or b) multipart messages. What they would like to do is do a POST to a url with only the data from the file.
Something like this can be done in curl:curl -d @google.png http://server/postcards/1/photo.json -X POST
I'd like the uploaded photo to put into the photo attribute of the postcards model and into the right directory.
I'm doing something like this in the controller but the image is corrupted in the directory. I am doing a manual renaming of the file to a "png" for now:
def PostcardsController < ApplicationController
...
# Other RESTful methods
...
def photo
@postcard = Postcard.find(params[:id])
@postcard.photo = request.body
@postcard.save
end
The model:
class Postcard < ActiveRecord::Base
mount_uploader :photo, PhotoUploader
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可以完成的,但您仍然需要客户端发送原始文件名(如果您对类型进行任何验证,则还需要发送内容类型)。
现在您可以使用 And 来设置照片
并使用
&type=image/png
指定内容类型。This can be done but you will still need your clients to send the orignal filename (and the content-type if you do any validation on the type).
And now you can set the photo using
And to specify the content-type use
&type=image/png
.