我使用插件和Django作为后端。在我试图集成同一页面上的多个文件上载小部件之前,一切都进行得很好。
第一个问题
首先,我不知道我应该改变什么,尽管医生。页面中提到的这是原始的main.js文件。我不是jQuery专家,我无法准确地理解我需要改变什么。
我很高兴看到这个main.js的例子被重写了。
第二问题
为我在一个页面上设置多个小部件的全部目的是显示不同的元素。这就是为什么,在我的后端,upload/view/ url变成了upload/view/(?P<pk>\d+)/。
例如,如果我想在一页中显示包含多张照片的两张相册,我希望我的页面调用upload/view/1和upload/view/2
后端部分运行良好,因为调用那里的urls返回正确的json答案。
但是对于ajax部分,我不知道该做什么。在main.js文件中,我可以看到应该在哪里更改它,但不知道如何做:
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
//url: $('#fileupload').fileupload('option', 'url'),
url: '/upload/view/',
dataType: 'json',
context: $('#fileupload')[0]任何帮助都将不胜感激。谢谢。
发布于 2015-07-23 13:44:59
您需要注释或删除代码的以下部分:
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
//url: 'server/php/'
});
// Enable iframe cross-domain access via redirect option:
$('#fileupload').fileupload(
'option',
'redirect',
window.location.href.replace(
/\/[^\/]*$/,
'/cors/result.html?%s'
)
);并放置这一行,允许您根据fileupload类设置多个上载小部件:
$('.fileupload').each(function () {
$(this).fileupload({
dropZone: $(this)
});
});发布于 2015-07-23 14:23:43
好的,
基于@Hackerman的答案和其他脑力提取器,下面是最后的工作main.js文件:
$(function () {
'use strict';
// cpt will count (0-based) number of fileupload forms in the page
var cpt = 0;
$('.fileupload').each(function () {
$(this).fileupload({
dropZone: $(this)
});
// Load existing files:
$(this).addClass('fileupload-processing');
$.ajax({
url: '/upload/view/' + $(this).children("input[name='folder_element']").val(),
dataType: 'json',
context: $('.fileupload')[cpt] // select the right form
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, null, {result: result});
});
cpt += 1;
});
});和对应于两个表单的HTML:
<form class="fileupload" method="post" action="." enctype="multipart/form-data">
<input type="hidden" name="folder_element" value="1">
...
</form>
<form class="fileupload" method="post" action="." enctype="multipart/form-data">
<input type="hidden" name="folder_element" value="2">
...
</form>我不是jQuery专家,但似乎需要有一个count变量才能在ajax请求中传递良好的上下文。
发布于 2015-08-27 23:42:29
试试这个(9.11.1):
表格:
<form data-id="1" .....
<form data-id="2" .....复制服务器(本例中为PHP文件夹),并将它们命名为php1和php2。然后,main.js文件就变成了(或多或少地保持原始代码的真实性):
$(function () {
'use strict';
// Initialize the jQuery File Upload widget:
$('.fileupload').each(function () {
$(this).fileupload({
dropZone: $(this)
});
$(this).fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: 'server/php'+$(this).attr('data-id')+'/'
});
// Load existing files:
$(this).addClass('fileupload-processing');
$.ajax({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: $(this).fileupload('option', 'url'),
dataType: 'json',
context: $(this)[0]
}).always(function () {
$(this).removeClass('fileupload-processing');
}).done(function (result) {
$(this).fileupload('option', 'done')
.call(this, $.Event('done'), {result: result});
});
});
// Enable iframe cross-domain access via redirect option:
$('.fileupload').fileupload(
'option',
'redirect',
window.location.href.replace(
/\/[^\/]*$/,
'/cors/result.html?%s'
)
);
});https://stackoverflow.com/questions/31587773
复制相似问题