- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在,在我的 Rails 应用程序中,我正在使用 Carrierwave 将文件上传到 Amazon S3。我正在使用文件选择器和表单来选择和提交文件,效果很好。
但是,我现在正在尝试从 iPhone 应用程序发帖,并且正在接收文件的内容。我想使用这些数据创建一个文件,然后使用 Carrierwave 上传它,这样我就可以获得正确的路径。
May 文件模型包括:
path
file_name
id
user_id
其中 path 是 Amazon S3 url。我想做这样的事情来构建文件:
data = params[:data]
~file creation magic using data~
~carrierwave upload magic using file~
@user_id = params[:id]
@file_name = params[:name]
@path = path_provided_by_carrierwave_magic
File.build(@user_id, @file_name, @path)
真的很想有人为我指明正确的方向。谢谢!
最佳答案
这是我通过载波从 iOS 应用程序执行上传到 s3 的代码:
首先是照片模型
class Photo
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :image, PhotoImageUploader
field :title, :type => String
field :description, :type => String
end
Api::V1::PhotosController 中的第二个
def create
@photo = current_user.photos.build(params)
if @photo.save
render :json => @photo.to_json, :status=>201
else
render :json => {:errors => @photo.errors}.to_json, :status=>403
end
end
然后使用 AFNetworking 从我的 iPhone 应用程序调用
-(void) sendNewPhoto
{
NSURL *url = [NSURL URLWithString:@"http://myserverurl.com"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:_photoTitle.text, @"title", _photoDescription.text, @"description",nil];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSString *endUrl = [NSString stringWithFormat:@"/api/v1/photos?auth_token=%@", [[User sharedInstance] token]];
NSData *imageData = UIImageJPEGRepresentation(_photo.image, 1.0);
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:endUrl parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"image.jpg" mimeType:@"image/jpg"];
}];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error creating photo!");
NSLog(@"%@", error);
}];
[operation start];
}
在 JSON 响应中,我可以获得 Photo 的新实例,其 image.url 属性设置为 s3 中的 url。
关于ruby-on-rails - 载波程序化上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12274180/
编辑:我的文件仅按照缓存而非 store_dir 的规定存储 我花了几个小时试图让它工作,Carrierwave 在本地工作,但在我的生产中,它们只保存在 Cache_dir 中,并且表单没有将我重定
在我的 rails 应用程序中,我使用 Carrierwave 在 Amazon S3 上上传图像。我想指向现有的 Amazon S3 图像,而无需重新上传图像。例如,如果我在 处有一个现有的 Ama
我有一个 发帖 模型和 播客模型。两个模型都有一个标题为:图像的属性。我正在使用一个 Carrierwave上传器(名为 ImageUploader)来处理这两种模型。在开始生产之前,我有两个问题。
您好,我正在关注 carrierwave documentation但是我必须把这部分放在哪里? u = User.new(params[:user]) u.save! u.avatars[0].ur
当前在Carrierwave中,在创建像这样的不同版本时上传了foo.png这样的文件之后: class ImageUploader [500, 500] end end 导致文件上传为: th
我有 RailsCasts 中描述的类似模型: 应用程序/模型/resident.rb: class Resident < ActiveRecord::Base include PhotoConc
这是一个类似的案例,但没有解决方案 CarrierWave extension_white_list doesn't seem to work Carrierwave 提供了一个功能 extensio
我想要 Carrierwave生成图像的几个版本,一个是 png 格式,一个是 jpeg 格式,但我似乎无法让它工作。我意识到这里和其他地方都提出了几个类似的问题,但我未能找到解决方案。粗略地说,这就
我正在通过设置 Model#filename = File.open(etc) 来测试我的文件上传对于带有 RSpec 的模型是否正确 当我的规范运行时,一切都很棒。但在测试套件完成后,文件仍保留在我
我有一个安装 Carrierwave uploader 的简单模型。 Ruby 版本 2.2.3、Ruby on Rails 4.2.4、Imagemagick 安装在 mac (10.11.1 El
我想下载在 aws s3 上上传的文件: 控制者 def download send_file my_file.url end 实际上我已经尝试了类似帖子中的所有代码: send_file o
我有一个 Catalog它有一个名为 file 的属性: class Catalog 但是当我单击该链接时,它只会给我一个路由错误 Routing Error No route matches [G
我有这个规范: post :create, :room_id => @room.id, :image => Rack::Test::UploadedFile.new(path, 'text/jpg')
看了好几篇相关的帖子和解决方案,还是想不通,还是返回“Excon::Errors::SocketError 在/postsgetaddrinfo: nodename 或 servname 提供,或
我有型号 Document : class Document true end 还有两个类继承自 Document : License : class License {"certificate"=
我已经成功安装了 Carrierwave。我可以从终端看到它:gem list carrierwave => *** LOCAL GEMS *** carrierwave (1.0.0.rc) 但我仍
我正在使用: - carrierwave 0.6.0 - rails 3.2.1 - mini_magick 3.4 - ruby 1.9.2p290 在上传图片的过程中,我想调整上传图片的大小: (
我想 stub carrierwave以防止它在我的测试期间在网络上获取图像。我将如何 stub 以实现这一目标? 我的爬虫解析远程网页,并将一个图像 url 保存到模型中。 Carrierwave
这里我有两个模型:User 和 Book用户有头像要上传,书有封面要上传我已阅读有关上传图片的 railscast,但我不知道应该如何处理不同类型的图片? (在这种情况下,用户的头像和书籍的封面) 我
我在尝试将 S3 服务与雾和 Jquery 文件上传 (https://github.com/blueimp/jQuery-File-Upload) 一起使用时遇到了这个问题 错误 Excon::Er
我是一名优秀的程序员,十分优秀!