Can't download and rename a jpg image.Instead it downloads a corrupted jpg file, that if you change the extension to ".txt" you can see the path of the image in there.
The original code that I had working was the following:
function downloadImg(id){
var deferred = $q.defer();
$http({
method: 'GET',
url: API_URL + 'image/' + id
}).then(function(res){
console.log(res.data);
if(res.data && res.data.length > 0){
window.location.href = res.data;
}
deferred.resolve();
}, function(err){
console.log(err);
deferred.reject();
})
return deferred.promise;
}
So, I just want to rename the image before or after download it. I end up with this code, that the only thing that is doing is setting the image path into a file or in this case [object Object] in the file(depending on the code I'm using I endup with object... or the image path...).
function downloadImg(id){
var deferred = $q.defer();
$http({
method: 'GET',
url: API_URL + 'image/' + id,
responseType: 'blob'
}).then(function(res){
console.log(res.data);
var file = new Blob([res], {type: 'image/jpeg;charset=UTF-8'});
var fileURL = URL.createObjectURL(file);
var a = document.createElement("a");
a.href = fileURL;
a.download = "newName.jpg";
a.click();
deferred.resolve();
}, function(err){
console.log(err);
deferred.reject();
})
return deferred.promise;
}
Also I tried setting responseType:'arraybuffer'
and blob
as well as tried to using FileSaver.saveAs
and tried to create a blob from base64 found at : http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript
My image url is something like (I replace some things with "personalName" because I can't share the image): https://personalName.personalName.com/component/get?id=cbd6a354-1700-11e8-b451-0a540af4044a&username=personalName&apiKey=a393ceba-1350-11e6-9382-f23c91df2143
I expected to have the original jpg image, but with the new name.
Thanks.