1.前端通过formData的方式批量增加图片或文件
for (var i = 0; i < form_img_list.length; i++) {
formData.append('image_file', form_img_list[i]);
}
AJAX写法
$('#submit_save_b').click(function (e) {
//formData 增加值
e.preventDefault()
console.log(form_img_list,33333);
for (var i = 0; i < form_img_list.length; i++) {
formData.append('image_file', form_img_list[i]);
} formData.append('advertiser', $('#base_username_id').text());
formData.append('campaign_name', $('#adv_name_value').val());
formData.append('campaign_id', $('#adv_plan_value').val());
formData.append('data', $('#adv_des_value').val());
formData.append('title', $('#adv_title_value').val());
formData.append('ad_imp_type', $('#ad_imp_type').val());
formData.append('landing_url', $('#adv_landing_value').val());
formData.append('enable', 1);
formData.append('remove_img_list', remove_img_list); console.log(formData); var checkText = $("#id_select_ad_campaing").find("option:selected").text();
if (checkText == '请选择广告计划:') {
alert('请选择广告计划');
return false;
} var csrftoken = getCookie('csrftoken');
$.ajax({
cache: false,
type: 'POST',
data: formData,
url: '/advertisement_list/',
traditional: true, //为必须内容
//dataType:'json', //data为json时必须
processData: false, //为必须内容
contentType: false, //为必须内容
success: function (data, textStatus, jqXHR) {
console.log(data);
if (jqXHR.status == 201) {
$('.page-content').fadeOut(300,function(){
$('.center').fadeIn(300)
})
show_list_table();
$('#Adver_Cell_Modal').modal('hide');
form_ad_campaing_reset();
} },
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
error:function(data){
console.log(222)
}
});
});
View写法
#获取多个文件
upload_images = request.FILES.getlist('image_file')
for a_index, afile in enumerate(upload_images):
#调用自定义上传文件方法
f_name, f_path = oss_storage.handle_uploaded_file(afile)
# 保证文件名不重复
current_time = time.time()
current_time = '{}_{}'.format(current_time, str(random.randint(1, 100))) image_url = 'http://liquid-xad.oss-cn-beijing.aliyuncs.com/{}/{}'.format('upload_test',
'{}_{}'.format(
current_time,
str(f_name))) oss_path = 'upload_test/{}'.format('{}_{}'.format(current_time, str(afile)))
f_path_name = '{}{}'.format(f_path, f_name)
upload_status = oss_storage.upload(oss_path, f_path_name)
print(upload_status) advertismen.ad_imp_type = p_ad_imp_type
advertismen.img_url = '' if a_index == 0:
advertismen.img_url_2 = image_url
advertismen.save()
os.remove(f_path_name) if a_index == 1:
advertismen.img_url_3 = image_url
advertismen.save()
os.remove(f_path_name) if a_index == 2:
advertismen.img_url_4 = image_url
advertismen.save()
os.remove(f_path_name) advertismen.save()
自定义上传方法
class Oss_Storage(object):
def handle_uploaded_file(self,f):
path = os.path.abspath(MEDIA_ROOT)
file_path = '{}/file/'.format(path)
BASE_DIR = file_path
try:
path = BASE_DIR
if not os.path.exists(path):
os.makedirs(path)
else:
file_name = str(path + f.name)
destination = open(file_name, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
except Exception as e:
print(e)
return f.name, path