我正在尝试将不同表单中的文件上传到服务器。问题是上传方法无法正常工作。神秘之处在于,如果我在firebug上使用断点进行调试,它可以正常工作。下面是我的html块:
<form class="omics_form" enctype="multipart/form-data">
<div class="form_block">
<div class="form_header">Select photo</div>
<input type="file" class="photo_file" accept="image/*">
<a href="#" class="remove_omics_just">clear</a>
</div>
<div class="form_block">
<div class="form_header">Description</div>
<input type = "text" class="photo_descr">
</div>
</form>
<form class="clinical_form" enctype="multipart/form-data">
<div class="form_block">
<div class="form_header">Select photo</div>
<input type="file" class="photo_file" accept="image/*">
<a href="#" class="remove_clinical_just">clear</a>
</div>
<div class="form_block">
<div class="form_header">Description</div>
<input type = "text" class="photo_descr">
</div>
</form>
现在我的jquery是这样的(请注意,可能有两种以上的形式):
$.each(all_clinical_forms, function(index, value){
if (extension === 'jpg' || extension === 'png' || extension === 'bmp' || extension === 'tif') {
formData.append("image_file", image_file);
var JSON_object = {
"submission_id": submission_id,
"id": clinical_id,
"descr": description,
"is_clinical": "true"
};
var JSON_string = JSON.stringify(JSON_object);
$.ajax({
url: "/delegate/UploadPhoto?JSON=" + JSON_string,
type: 'POST',
dataType: "JSON",
beforeSend: function(xhr){
if (xhr.overrideMimeType)
{
xhr.overrideMimeType("application/json");
}
},
success: function(data) {
},
error: function(xhr, textStatus, errorThrown) {
alert("Error trying upload the file.");
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
} else {
alert('The file '+file_name+' is not an image format.');
}
}
$.each(all_omics_forms, function(index, value){
if (extension === 'jpg' || extension === 'png' || extension === 'bmp' || extension === 'tif') {
formData.append("image_file", image_file);
var JSON_object = {
"submission_id": submission_id,
"id": omics_id,
"descr": description,
"is_clinical": "false"
};
var JSON_string = JSON.stringify(JSON_object);
$.ajax({
url: "/delegate/UploadPhoto?JSON=" + JSON_string,
type: 'POST',
dataType: "JSON",
beforeSend: function(xhr){
if (xhr.overrideMimeType)
{
xhr.overrideMimeType("application/json");
}
},
success: function(data) {
},
error: function(xhr, textStatus, errorThrown) {
alert("Error trying upload the file.");
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
}
正如我所说,当我调用servlet时,只上传了一个图像。神秘的是,如果我通过firebug在每个ajax调用上设置一个断点,那么图像上传得很好。也许这只是时间问题?我真的在这里堆得很困惑。 有任何帮助或建议吗?