在同一个请求中上传多个文件
Uploading multiple files in the same request
我需要创建一个 POST,我可以在同一个请求中上传多个文件,但我不知道如何用 grape 写这个。现在只上传一个文件这就是我正在做的并且工作正常:
desc 'Creates a new attachment.'
params do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
post do
attachment = Attachment.new
attachment.file = ActionDispatch::Http::UploadedFile.new(params[:file])
attachment.save!
attachment
end
Swagger 向我展示了这个:
我正在考虑做这样的事情:
desc 'Creates a new attachment.'
params do
requires :file, :type => Array[Rack::Multipart::UploadedFile], :desc => "Attachment File."
end
但看起来不太好:
我也试过:
params do
optional :attachments, type: Array do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
end
也不是什么好结果。
正确的处理方式是什么?
经过一些测试我找到了答案,我会把它留在这里:
params do
requires :first_name, type: String, desc: "User first name"
requires :last_name, type: String, allow_blank: false, desc: "User last name"
optional :attachments, type: Array do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Profile pictures."
end
end
你不能用 swagger 测试这个(或者至少我没有找到这样做的方法),但你可以使用这个 curl:
curl -v -F "first_name=John" -F "last_name=McTest" -F "attachments[][file]=@first_picture.png" -F "attachments[][file]=@second_picture.jpg" http://url/api/users
我需要创建一个 POST,我可以在同一个请求中上传多个文件,但我不知道如何用 grape 写这个。现在只上传一个文件这就是我正在做的并且工作正常:
desc 'Creates a new attachment.'
params do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
post do
attachment = Attachment.new
attachment.file = ActionDispatch::Http::UploadedFile.new(params[:file])
attachment.save!
attachment
end
Swagger 向我展示了这个:
我正在考虑做这样的事情:
desc 'Creates a new attachment.'
params do
requires :file, :type => Array[Rack::Multipart::UploadedFile], :desc => "Attachment File."
end
但看起来不太好:
我也试过:
params do
optional :attachments, type: Array do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
end
也不是什么好结果。
正确的处理方式是什么?
经过一些测试我找到了答案,我会把它留在这里:
params do
requires :first_name, type: String, desc: "User first name"
requires :last_name, type: String, allow_blank: false, desc: "User last name"
optional :attachments, type: Array do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Profile pictures."
end
end
你不能用 swagger 测试这个(或者至少我没有找到这样做的方法),但你可以使用这个 curl:
curl -v -F "first_name=John" -F "last_name=McTest" -F "attachments[][file]=@first_picture.png" -F "attachments[][file]=@second_picture.jpg" http://url/api/users