我想将图像作为附件发送。
我的代码:
resp = FileResponse(open(fullImgPath, "rb"))
resp['Content-Disposition'] = 'attachment; filename="{}"'.format(os.path.basename(fullImgPath))
resp["Content-Type"]="image/%s"%('png' if fullImgPath.endswith('png') else 'jpeg')
return resp
它确实有效,如果我通过requests. 但是当我通过浏览器(chrome 和 firefox)下载文件时,文件已损坏。
我如何通过浏览器(javascript)做到这一点:
$.get(requestUrl)
.success(function(data, textStatus, request){
SaveBlob(data, "1,jpeg", "image/jpeg")
}
})
function SaveBlob(blob, fileName, contentType) {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([blob], { "type" : contentType }));
a.download = fileName;
a.dispatchEvent(new MouseEvent('click'));
}
它以前有效!今天我发现我得到的文件已损坏。但是,存储在服务器上的文件是普通图像。
怎么了?
慕尼黑5688855
相关分类