当我使用upload component时,我不知道如何接收服务器返回的json,例如:
{"data":"", "state":2000, "message":"upload fail"}
当我console.log("file_response:",file.response)
时,它显示响应:{"status": "success"}
,而不是我要接收的json数据。
class Verify extends React.Component {
constructor(props){
super(props);
this.state = {
merId:'-1',
verifyStat:0,
loading: false,
fileList: [{
uid: '-1',
name: 'license.JPG',
status: 'done',
response: '{"status": "success"}',
}],
};
}
handleChange = (info) => {
let fileList = info.fileList;
// 1. Limit the number of uploaded files
// Only to show two recent uploaded files, and old ones will be replaced by the new
fileList = fileList.slice(-2);
// 2. Read from response and show file link
fileList = fileList.map((file) => {
if (file.response) {
// Component will show file.url as link
console.log("file_response_json:",file.response)
file.url = file.response.url;
}
return file;
});
}
const licenUpload = {
multiple: true,
data: this.state.merId+image_suffix_name,
//action: '//172.20.22.182:8080/merchant/upload',
action: path,
onChange: this.handleChange,
beforeUpload: beforeUpload,
};
后台服务器upload.py:
def uploadFile(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
print(request.FILES)
q = request.POST
a = ''
for key in request.POST:
a = a + q.__getitem__(key)
if form.is_valid():
image_addr = handle_uploaded_file(request.FILES['file'],a)
print(image_addr)
return JsonResponse({"data":image_addr, "state":0, "message":"ok"})
else:
form = UploadFileForm()
return JsonResponse({"data":"", "state":2000, "message":"upload fail"})