Closed DaMinaup6 closed 9 years ago
Check the browser's network tab and if it is sending all the files in the request body then it's a server side issue, you can create a stackoverflow question.
I finally found a way to solve my problem. Thanks for @danialfarid's awesome ng-file-upload.
My problem was I couldn't send all selected files. My solution was
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
});
}
Then in my rails 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
I created an array to store all my files from params
.
I tried to use a column with mount_uploaders
of carrierwave to store an array of files, but it didn't work. So I create a file table called attaches
to store my files
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
where attachable
is used to store the post id and type. (Here my attachments belong to some post in my forum.)
Here is some details about setting if needed
attach.rb
(model)
class Attach < ActiveRecord::Base
mount_uploader :filename, AttachUploader
belongs_to :attachable, :polymorphic => true
end
post.rb
(model)
class Post < ActiveRecord::Base
has_many :attaches, as: :attachable, dependent: :destroy
end
post_serializer.rb
class PostSerializer < ActiveModel::Serializer
has_many :attaches
end
attach_serializer.rb
class AttachSerializer < ActiveModel::Serializer
attributes :url, :name
def url
object.filename.url
end
def name
object.filename_identifier
end
end
Then in html
file can have a row code
<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>
where my default attachments are used for images.
I'm trying to combine ng-file-upload and carrierwave to upload multiple files, but the controller on server side receives only one file (the last item of the selected files).
Client side
html
js
console.log(files)
printsArray[File, File, ...]
(Browser: FireFox). So on client side it does get the selected files.Server side
posts_controller.rb
where
@post.attaches
is the attachments of a post (reference).I want to store an array of files into
@post.attaches
, butparams[:file]
contains only one file of the selected files.puts params[:file]
prints:This shows that there is only one file in
params[:file]
. How could I solve this problem?Here is my
post.rb
model andattach_uploader.rb
(created by carrierwave) for reference if needed: post.rbattach_uploader.rb
and
@post.attaches
column in databaseposts
is added by