如果是Python客户端上传Post
可参考本博客:https://blog.csdn.net/qq_21237549/article/details/123408690
前端js上传文件
<div class="ac-game-settings-acwing">
<br>
<input type="file" name="file" id="file_select">
<input type="button" value="上传" class="file_upload">
</div>
add_listening_events() {
console.log('ok');
let outer = this;
this.add_listening_events_login();
this.add_listening_events_register();
}
add_listening_events_login() {
let outer = this;
console.log('123');
this.$login_register.click(function () {
console.log('click register');
outer.register();
});
this.$login_submit.click(function () {
outer.login_on_remote();
});
this.$upload_file.click(function () {
outer.FileUpload();
});
}
FileUpload() {
var form_data = new FormData();
var file_info = $('#file_select')[0].files[0];
form_data.append('file', file_info);
if (file_info == undefined) {
alert('你没有选择任何文件');
return false;
}
$.ajax({
url: target_url + "/files/file_up/",
type: 'POST',
data: form_data,
processData: false, // tell jquery not to process the data
contentType: false, // tell jquery not to set contentType
success: function (callback) {
alert('上传成功');
}
});
}
后端获取ajax
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
@csrf_exempt
def upload_ajax(request):
print(request.FILES.get('file').name)
if request.method == 'POST':
file_obj = request.FILES.get('file')
import os
# JS_PATH=/home/fjtx/ai_asian_back/demo/static/js/
path1 = os.path.abspath(os.path.join(os.getcwd()))
print(path1)
#需要存储的地址
f = open(os.path.join(path1, 'static', 'file', file_obj.name), 'wb')
print(file_obj,type(file_obj))
for chunk in file_obj.chunks():
f.write(chunk)
f.close()
print('11111')
return JsonResponse({
'result': "success"
})
js前端进行上传
Post传递参数
参考:https://blog.csdn.net/a582816317/article/details/79100754
前端js代码
FileUpload() {
var form_data = new FormData();
var file_info = $('#file_select1')[0].files[0];
console.log(file_info)
form_data.append('file', file_info);
form_data.append('username', this.root.settings.username);//传递参数
if (file_info == undefined) {
alert('你没有选择任何文件');
return false;
}
$.ajax({
url: target_url + "/files/file_up/",
type: 'POST',
data: form_data,
processData: false, // tell jquery not to process the data
contentType: false, // tell jquery not to set contentType
success: function (callback) {
alert('上传成功');
}
});
}
后端python代码
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
@csrf_exempt
def upload_ajax(request):
print(request.FILES.get('file').name)#接收文件
username=request.POST.get('username')#接收参数
print(username)
if request.method == 'POST':
file_obj = request.FILES.get('file')
import os
# JS_PATH=/home/fjtx/ai_asian_back/demo/static/js/
path1 = os.path.abspath(os.path.join(os.getcwd()))
print(path1)
#需要存储的地址
f = open(os.path.join(path1,'demo', 'static', 'file', file_obj.name), 'wb')
for chunk in file_obj.chunks():
f.write(chunk)
f.close()
return JsonResponse({
'result': "success"
})
Django返回列表
前端