1
我正在开发一个需要做多个文件上传的CakePHP Web应用程序。Ajax文件上传问题
我已经实现了一个Ajax File Upload插件来帮助我做到这一点。但是,由于我的表单元素必须提交给Controller,所以我不能将它设置为我自己的PHP上传脚本action
。
有没有办法让我在我的JavaScript中设置PHP上传脚本,而让表单提交给它的控制器?
这是我正在使用的JS脚本。
$(function(){
var ul = $('#attachments_plugin ul');
$('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
$(this).parent().find('input').click();
});
// Initialize the jQuery File Upload plugin
$('#attachments_plugin').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// 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) {
var tpl = $('<li class="working"><input type="text" value="0" data-width="20" data-height="20"'+
' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i>');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded/data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
}
});
// Prevent the default action when a file is dropped on the window
$(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes/1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes/1000000).toFixed(2) + ' MB';
}
return (bytes/1000).toFixed(2) + ' KB';
}
});
该脚本中的某处我需要告诉它哪个PHP脚本用于上传。我只是不确定在哪里以及如何。任何帮助将不胜感激。谢谢。
谢谢您的答复。我不确定你是否理解我的问题。我知道如何将表单提交给它的控制器,我需要知道的是如何告诉JavaScript哪些PHP上传脚本使用..因为我的表单'action'指向控制器。 – kingLoosh
我的道歉同样,请尝试 $('#fileupload')。fileupload({/ url /'/ path/to/upload/handler.json' }); –
修改了代码。请测试,让我知道它是否工作 –