Django + Ajax + FormData: 上传的文件是unicode数组
Django + Ajax + FormData: uploaded file is unicode array
我无法将图片上传到服务器。那里的所有属性直到图像。在 request.FILES 中,它们是,但仅作为文件名的 unicode 数组。
而这个错误:
AttributeError: 'unicode' object has no attribute 'read'
这是我的看法:
for _f in request.FILES:
print(_f)
for _fi in _f:
print(_fi)
photo = PostPhoto.objects.create(photo = _f, name = str(_f))
photo.save()
destination = open('media/photos/'+str(photo.pk)+'.jpeg', 'w')
for chunk in _f.read():
destination.write(chunk)
destination.close()
print(photo)
post.photos = photo
这是 js:
if (file.length <= 10) {
if (hasExtension(file, ['jpeg','jpg'])) {
console.log('files are correct');
var message = document.getElementById('search_textarea').value;
console.log('text');
var body_data = new FormData();
body_data.append('text', message);
body_data.append('loc_lat', elements[i].latitude);
body_data.append('loc_lon', elements[i].longitude);
body_data.append('loc_name', results[i].name);
body_data.append('loc_addr', results[i].formatted_address);
body_data.append('types', results[i].types);
body_data.append('action', null);
for(var k = 0; k <= (file.length - 1); k++) {
console.log(file[k]);
body_data.append(k, file[k], file[k].name);
}
this.$.ajaxNewPost.body = body_data;
this.$.ajaxNewPost.contentType = false;
this.$.ajaxNewPost.generateRequest();
console.log('ajax sended');
} else {
console.log('incorrect files');
}
} else {
alert('too match files');
}
我该如何解决?我试过 post 通过 tastypie,但我也无法保存图像,因为 tastypie 返回给我这个请求的字节文件
更新代码:
for _f in request.FILES:
photo = PostPhoto.objects.create(photo=_f, name=str(_f))
photo.save()
destination = open('media/photos/' + str(photo.pk) + '.jpeg', 'w')
for _file in request.FILES.get(_f):
for chunk in _file:
destination.write(chunk)
post.photos = photo
destination.close()
在您的代码中 for _f in request.FILES:
_f 是一个 unicode 类型,这意味着它只是一个字符串。
尝试用_f = request.FILES.get('your_parameter_name')
代替
我无法将图片上传到服务器。那里的所有属性直到图像。在 request.FILES 中,它们是,但仅作为文件名的 unicode 数组。 而这个错误:
AttributeError: 'unicode' object has no attribute 'read'
这是我的看法:
for _f in request.FILES:
print(_f)
for _fi in _f:
print(_fi)
photo = PostPhoto.objects.create(photo = _f, name = str(_f))
photo.save()
destination = open('media/photos/'+str(photo.pk)+'.jpeg', 'w')
for chunk in _f.read():
destination.write(chunk)
destination.close()
print(photo)
post.photos = photo
这是 js:
if (file.length <= 10) {
if (hasExtension(file, ['jpeg','jpg'])) {
console.log('files are correct');
var message = document.getElementById('search_textarea').value;
console.log('text');
var body_data = new FormData();
body_data.append('text', message);
body_data.append('loc_lat', elements[i].latitude);
body_data.append('loc_lon', elements[i].longitude);
body_data.append('loc_name', results[i].name);
body_data.append('loc_addr', results[i].formatted_address);
body_data.append('types', results[i].types);
body_data.append('action', null);
for(var k = 0; k <= (file.length - 1); k++) {
console.log(file[k]);
body_data.append(k, file[k], file[k].name);
}
this.$.ajaxNewPost.body = body_data;
this.$.ajaxNewPost.contentType = false;
this.$.ajaxNewPost.generateRequest();
console.log('ajax sended');
} else {
console.log('incorrect files');
}
} else {
alert('too match files');
}
我该如何解决?我试过 post 通过 tastypie,但我也无法保存图像,因为 tastypie 返回给我这个请求的字节文件
更新代码:
for _f in request.FILES:
photo = PostPhoto.objects.create(photo=_f, name=str(_f))
photo.save()
destination = open('media/photos/' + str(photo.pk) + '.jpeg', 'w')
for _file in request.FILES.get(_f):
for chunk in _file:
destination.write(chunk)
post.photos = photo
destination.close()
在您的代码中 for _f in request.FILES:
_f 是一个 unicode 类型,这意味着它只是一个字符串。
尝试用_f = request.FILES.get('your_parameter_name')
代替