gpt4 book ai didi

javascript - jquery和ajax上传图片两次

转载 作者:行者123 更新时间:2023-12-02 15:54:37 26 4
gpt4 key购买 nike

我有一个通过 ajax 上传图像的 jQuery 脚本,我注意到由于某种原因,它上传图像两次。一旦在ajax步骤中,它们作为隐藏值嵌入到表单中,然后当有人单击提交表单时,上传过程会再次开始,而实际上不应该开始,因为图像已经在服务器上。我看到它是因为我使用 Chrome,当我点击提交时,它在左下角再次显示“正在上传(19%)..”等。这不是我的脚本,而且我对 jQuery 很不擅长,所以我不知道出了什么问题。这里有什么明显的东西可以让它在提交时再次上传吗?

function media_sync(form_name, media_type, file_path, file_url, file_embed, nb_uploads, max_uploads, upload_id) {
var radio_buttons = [];
for (var i = 0; i < form_name.length; i++) {
if (form_name.elements[i].type == 'radio' && form_name.elements[i].checked == true) {
radio_buttons[form_name.elements[i].name] = form_name.elements[i].value;
}
}

var jsHttp = new JsHttpRequest();
var xmlHttp = GetXmlHttpObject();

if (jsHttp == null) {
alert("Browser does not support HTTP Request");
return;
}

var media_name = '';
switch (media_type) {
case 1:
media_name = 'ad_image';
break;
case 2:
media_name = 'ad_video';
break;
case 3:
media_name = 'ad_dd';
break;
}

var media_box_name = 'display_media_boxes_' + media_name;

file_embed = base64Encode(file_embed);
var url = relative_path + 'file_upload.php';
var action = url + '?do=add&media_type=' + media_type + '&file_url=' + file_url +
'&file_embed=' + file_embed + '&nb_uploads=' + nb_uploads + '&upload_id=' + upload_id;

var thumbnail_div = document.getElementById('display_media_boxes_' + media_name);
var new_content = document.createElement('div');

if (file_embed != '') {
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;

var results = response.split('|');

if (results[5] != '') {
alert(results[5]);
}
else {
new_content.innerHTML = results[3];

while (new_content.firstChild) {
thumbnail_div.appendChild(new_content.firstChild);
}

var hidden_div = document.getElementById('hidden_media_boxes');
var hidden_content = document.createElement('div');
hidden_content.innerHTML = '<input type="hidden" name="' + results[1] + '[]" id="hidden_' + results[2] + '" value="' + results[4] + '">';

while (hidden_content.firstChild) {
hidden_div.appendChild(hidden_content.firstChild);
}
nb_uploads++;
}

document.getElementById('item_file_url_' + media_type).value = '';
document.getElementById('item_file_embed_' + media_type).value = '';

if (nb_uploads >= max_uploads) {
document.getElementById('btn_upload_' + media_type).disabled = true;
document.getElementById('item_file_upload_' + media_type).disabled = true;
document.getElementById('item_file_url_' + media_type).disabled = true;
document.getElementById('item_file_embed_' + media_type).disabled = true;
}
document.getElementById('nb_uploads_' + media_type).value = nb_uploads;
}
};
xmlHttp.open("POST", action, true);
xmlHttp.send(null);
}
else {
jQuery.blockUI({
message: $('#uploading_image'),
css: {
top: '20%',
left: ($(window).width() - 400) /2 + 'px',
width: '400px'
}
});


var image_loading = document.createElement('div');
image_loading.innerHTML = '';
thumbnail_div.appendChild(image_loading);

jsHttp.onreadystatechange = function() {
if (jsHttp.readyState == 4) {
var response = jsHttp.responseText;

var results = response.split('|');

if (results[5] != '') {
alert(results[5]);
thumbnail_div.removeChild(image_loading);
setTimeout(jQuery.unblockUI, 500);
}
else {
new_content.innerHTML = results[3];

while (new_content.firstChild) {
thumbnail_div.replaceChild(new_content.firstChild, image_loading);
}
setTimeout(jQuery.unblockUI, 500);

var hidden_div = document.getElementById('hidden_media_boxes');
var hidden_content = document.createElement('div');
hidden_content.innerHTML = '<input type="hidden" name="' + results[1] + '[]" id="hidden_' + results[2] + '" value="' + results[4] + '">';

while (hidden_content.firstChild) {
hidden_div.appendChild(hidden_content.firstChild);
}
nb_uploads++;
}

document.getElementById('item_file_url_' + media_type).value = '';
document.getElementById('item_file_embed_' + media_type).value = '';

if (nb_uploads >= max_uploads) {
document.getElementById('btn_upload_' + media_type).disabled = true;
document.getElementById('item_file_upload_' + media_type).disabled = true;
document.getElementById('item_file_url_' + media_type).disabled = true;
document.getElementById('item_file_embed_' + media_type).disabled = true;
}
document.getElementById('nb_uploads_' + media_type).value = nb_uploads;

for (var i = 0; i < form_name.length; i++) {
if (form_name.elements[i].type == 'radio' && radio_buttons[form_name.elements[i].name] == form_name.elements[i].value) {
form_name.elements[i].checked = true;
}
}
}
};

jsHttp.open("POST", action, true);
jsHttp.send({file: file_path});
}
jQuery('#upload_form').attr('target', '_self');
}

HTML 表单非常基本,有一堆隐藏值字段,它们只是变量和图像上传部分,如下所示:

<form id="upload_form" action="new.php" method="post" enctype="multipart/form-data" name="ad_create_form">

<input class="upload_button" type="file" name="item_file_upload_1" id="item_file_upload_1"
onchange="media_sync(ad_create_form, 1,
this.form.item_file_upload_1,
document.getElementById('item_file_url_1').value,
document.getElementById('item_file_embed_1').value,
document.getElementById('nb_uploads_1').value,
5,
'000000015');" />

<input name="finish" id="finish" value="Add" type="submit" class="btn">

</form>

如果有人能找出导致此问题的原因,非常感谢! :)

最佳答案

您有一个“onchange”,它会触发 media_sync 函数,然后运行 ​​Ajax 来发布数据。然后您还可以让表单本身提交数据。这就是为什么它要上传两次,一次用于 Ajax,一次用于表单提交。您需要选择其中之一而不是同时使用两者。我不确定您想要使用哪一个,因为这取决于您希望页面如何工作。我的猜测是,Ajax 就足够了,因此删除表单提交(因为表单中唯一的内容就是一个输入)。

关于javascript - jquery和ajax上传图片两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com