Kar*_*k K 11 jquery google-chrome ruby-on-rails jquery-file-upload
我正在尝试使用Blueimp的jQuery File Upload插件实现目录上传.Rails 4是我的后端,对于附件,我正在使用Carrierwave.
现在的问题是,jquery插件无法识别我正在上传的文件夹.我已经在输入字段中传递了webkitdirectory参数.有人可以帮我弄这个吗?
提前致谢.
这是application.js中的jQuery文件上载代码:
$('#fileupload').fileupload({
dataType: "script",
sequentialUploads: true,
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function(e, data) {
data.context = $(tmpl("template-upload", data.files[
0]))
data.context.addClass('working');
//$('.upload-status-box').addClass('working');
$('#Upload-Bar').append(data.context);
$('.upload-status-box').show();
// Listen for clicks on the cancel button
data.context.find('span.cancel-upload').click(
function() {
jqXHR.abort();
data.context.fadeOut(function() {
data.context.remove();
});
count = count - 1;
removeUploadStatusBoxOnCompletion();
});
var jqXHR = data.submit();
count = count + 1;
},
progress: function(e, data) {
if (data.context) {
$('.upload-status-box').show();
progress = parseInt((data.loaded / data.total) *
100);
var uploadMeta = parseInt(data.loaded / 1000000) +
"/" + parseInt(data.total / 1000000) +
" MB - " + progress + "%";
data.context.find('.progress-bar').css('width',
progress + '%');
data.context.find('.status').text(uploadMeta);
}
},
done: function(e, data) {
console.log(
'Your files have been uploaded successfully!'
);
// alert('Your files have been uploaded successfully! Depending on the file size, you might have to wait for a while before performing any actions.');
count = count - 1;
data.context.removeClass('working');
data.context.find('button.cancel-upload').hide();
removeUploadStatusBoxOnCompletion();
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的输入字段:
<div class="awesome-file-uploads" id="inline-upload-status">
<%= form_for [myfolder, Myfile.new], html: { multipart: true, :id => "fileupload" } do |f| %>
<%= f.file_field :attachment, multiple: true, id: "fileinput", style: "display:none;", "webkitdirectory"=> "", "directory"=> "" %>
<%= f.hidden_field :myfolder_parent_id, value: myfolder.id %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这是上传和处理文件夹内文件的代码:
$('#folderupload').change(function(e) {
e.preventDefault();
NProgress.done();
var items = e.target.files;
var paths = ""; //
// var myfolder_id = $(this).parent();
for (var i=0, file; file=items[i]; i++) {
paths += file.webkitRelativePath+"###";
} //
// uploadFiles(items, myfolder_id.data('inside'));
$("#paths").val(paths);
$("#folderupload").submit(); //
});
function uploadFiles(items, myfolder_id){
xhr = new XMLHttpRequest();
data = new FormData();
paths = "";
var AUTH_TOKEN = $('meta[name=csrf-token]').attr('content');
data.append('authenticity_token', AUTH_TOKEN);
// Set how to handle the response text from the server
xhr.onreadystatechange = function(ev){
console.debug(xhr.responseText);
};
for (var i=0, file; file=items[i]; i++) {
paths += file.webkitRelativePath+"###";
data.append(i, file);
}
data.append('paths', paths);
xhr.open('POST', "/myfolders/"+myfolder_id+"/create_from_folder", true);
xhr.send(this.data);
}
Run Code Online (Sandbox Code Playgroud)
将 jQuery 文件上传(UI 版本)与自定义服务器端上传处理程序结合使用
您可以通过访问https://github.com/blueimp/jQuery-File-Upload/wiki/Setup查看输出格式
| 归档时间: |
|
| 查看次数: |
2305 次 |
| 最近记录: |