处理完视频后,我传递了一个Javascript" Blob"对象,包含预览图像(JPEG格式)。我想通过ajax POST将图像发送到django的后端,并且我很难这样做。
我尝试在javascript中编码为base64,然后在python中解码并再次创建图像,但图像文件无法打开;它已经损坏或腐败。我包含了我在下面写的代码: Django:
def thumbnail_photo(request):
if request.POST:
data = request.POST.get('thumbnail')
convert = base64.b64decode(data)
image_result = open('thumbnail.jpeg', 'wb')
image_result.write(convert)
使用Javascript:
onPreviewAvailable: function(previewImageBlob) {
blob = previewImageBlob;
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
base64data = reader.result;
base64 = base64data;
base64 = window.btoa(base64);
data = {'thumbnail': base64};
$.ajax({
url: 'thumbnail_photo',
type: 'POST',
data: data,
dataType:'json',
success: function (data) {
}
});
});
}
我也尝试了以下内容:
blob = previewImageBlob;
var oReq = new XMLHttpRequest();
oReq.open("POST", thumbnail_photo, true);
oReq.onload = function (
};
oReq.send(blob);
但是,该值或者为空,或者我会收到一条错误消息,指出该文件无法发送。
我还尝试将blob附加到表单数据中:
var formData = new FormData();
formData.append('file', blob);
$.ajax({
url: 'thumbnail_photo',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(response) {
console.log('works');
$('#result').text(response);
}
});
任何帮助表示赞赏!!