I'm currently utilizing the FileStack API along with the filepicker gem in my project. The JavaScript snippet provided below is responsible for parsing a JSON response from the browser and forwarding it to the create action of the Attachment controller in Rails. My goal is to enhance this functionality to support the upload of multiple files, which would result in the creation of multiple attachment models. Ultimately, I aim for all the uploaded files to be saved upon form submission. While the existing code successfully handles single file uploads, I am seeking assistance to enable drag-and-drop functionality for multiple files. Any assistance on this matter would be highly appreciated.
<%= simple_form_for @attachment, :html=> { id: 'file_stack_form' } do |f| %>
<%= f.filepicker_field :title, multiple: 'true', onchange: 'onUpload(event)' %>
<% end %>
<script>
function onUpload(event) {
var fileNumber = event.fpfiles.length;
var url = event.fpfile.url;
var name = event.fpfile.filename;
for (var i = 0; i < fileNumber; i++) {
jQuery.ajax({
data: { "attachment[name]": name, "attachment[title]" : url },
type: 'post',
url: "/attachments"
});
success: $('#file_stack_form').submit();
}
}