use*_*072 7 ruby-on-rails carrierwave angularjs ng-file-upload
我正在尝试将ng-file-upload和carrierwave结合起来上传多个文件,但服务器端的控制器只接收一个文件(所选文件的最后一项).
HTML
<button type="file" ng-model="files" ngf-select ngf-multiple="true">Upload</button>
Run Code Online (Sandbox Code Playgroud)
JS
var upload = function (files, content) {
return Upload.upload({
url: 'MY_CONTROLLER_URL',
file: files, // files is an array of multiple files
fields: { 'MY_KEY': 'MY_CONTENT' }
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('files ' + config.file.name + ' uploaded. Response: ' + data);
}).error(function (data, status, headers, config) {
console.log('error status: ' + status);
});
};
Run Code Online (Sandbox Code Playgroud)
console.log(files)打印数组[文件,文件,...](浏览器:FireFox).所以在客户端,它确实获得了所选的文件.在ng-file-upload的GitHub页面上说它支持的文件数组html5.
posts_controller.rb
def create
@post = Post.new(post_params)
@post.attaches = params[:file]
@post.save
render json: @post
end
private
def post_params
params.require(:post).permit(:content, :file)
end
Run Code Online (Sandbox Code Playgroud)
@post.attaches帖子的附件在哪里,并params[:file]通过file参数从客户端发送Upload.upload.
我想存储一个文件数组@post.attaches,但params[:file]只包含所选文件的一个文件.
puts params[:file]打印:
#<ActionDispatch::Http::UploadedFile:0x007fddd1550300 @tempfile=#<Tempfile:/tmp/RackMultipart20150812-2754-vchvln.jpg>, @original_filename="Black-Metal-Gear-Rising-Wallpaper.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"Black-Metal-Gear-Rising-Wallpaper.jpg\"\r\nContent-Type: image/jpeg\r\n">
Run Code Online (Sandbox Code Playgroud)
这表明只有一个文件params[:file].我不确定这个参数的使用是否有任何问题.
我怎么能解决这个问题?
这是我的post.rb模型和attach_uploader.rb(由carrierwave创建)以供参考(如果需要):
post.rb
class Post < ActiveRecord::Base
mount_uploaders :attaches, AttachUploader
end
Run Code Online (Sandbox Code Playgroud)
attach_uploader.rb
class AttachUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Run Code Online (Sandbox Code Playgroud)
和@post.attaches数据库中的列posts添加
rails g migration add_attaches_to_posts attaches:json
Run Code Online (Sandbox Code Playgroud)
我终于找到了解决我的问题的方法。感谢Carrierwave和danialfarid出色的ng-file-upload。
我的问题是我无法发送所有选定的文件。我的解决方案是
var upload = function (files) {
var names = [];
for (var i = 0; i < files.length; ++i)
names.push(files[i].name);
return Upload.upload({
url: '/api/v1/posts',
file: files,
fileFormDataName: names
});
}
Run Code Online (Sandbox Code Playgroud)
然后在我的轨道上 controller.rb
file_arr = params.values.find_all { |value| value.class == ActionDispatch::Http::UploadedFile }
if @post.save
unless file_arr.empty?
file_arr.each { |attach|
@attach = Attach.new
@attach.filename = attach
@attach.attachable = @post
@attach.save
}
end
render json: @post
end
Run Code Online (Sandbox Code Playgroud)
我创建了一个数组来存储我的所有文件params。
我尝试使用带有载波mount_uploaders的列来存储文件数组,但它不起作用。所以我创建一个名为的文件表来存储我的文件attaches
class CreateAttaches < ActiveRecord::Migration
def change
create_table :attaches do |t|
t.string :filename
t.references :attachable, polymorphic: true
t.timestamps null: false
end
end
end
Run Code Online (Sandbox Code Playgroud)
其中attachable用于存储帖子 ID 和类型。(这里我的附件属于我论坛中的某个帖子。)
以下是有关设置的一些详细信息(如果需要)
attach.rb(模型)
class Attach < ActiveRecord::Base
mount_uploader :filename, AttachUploader
belongs_to :attachable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
post.rb(模型)
class Post < ActiveRecord::Base
has_many :attaches, as: :attachable, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)
post_serializer.rb
class PostSerializer < ActiveModel::Serializer
has_many :attaches
end
Run Code Online (Sandbox Code Playgroud)
attach_serializer.rb
class AttachSerializer < ActiveModel::Serializer
attributes :url, :name
def url
object.filename.url
end
def name
object.filename_identifier
end
end
Run Code Online (Sandbox Code Playgroud)
然后在html文件中可以有一行代码
<div ng-repeat="attach in post.attaches">
<img ng-src="{{attach.url}}" type="file" height="180" width="320" accept="image/*"/>
<a target="_self" ng-show="attach.url" href="{{attach.url}}" download="{{attach.name}}">{{attach.name}}<p></p></a>
</div>
Run Code Online (Sandbox Code Playgroud)
我的默认附件用于图像。
| 归档时间: |
|
| 查看次数: |
2285 次 |
| 最近记录: |