Trying to send an image as an attachment.
This is the code I am using:
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
The code works fine when downloading the file using requests
. However, when downloading the file via web browsers (such as Chrome and Firefox), the file ends up being corrupted.
Here's how you can download the file using JavaScript in a browser:
$.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'));
}
The previous method was working perfectly, but today I noticed that the file I downloaded appears to be corrupt. However, the image file stored on the server seems to be normal.
What could be causing this issue?