如何使用angularjs下载和重命名jpg图像

时间:2019-06-06 16:11:55

标签: html angularjs image download ansible

无法下载和重命名jpg图片。相反,它会下载损坏的jpg文件,如果将扩展名更改为“ .txt”,则可以在其中看到图片的路径。

我曾经使用的原始代码如下:

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;
    }

因此,我只想在下载图像之前或之后重命名该图像。 我得到的是这段代码,唯一要做的就是将图像路径设置到文件中,或者在这种情况下将文件中的[object Object]设置为文件(取决于我使用的代码,我最终将object ...或图片路径...)。

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;
    }

我也尝试设置responseType:'arraybuffer'blob,并尝试使用FileSaver.saveAs,并尝试从位于http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript的base64创建blob。

我的图片网址类似于(由于无法共享图片,我用“ personalName”替换了某些内容):https://personalName.personalName.com/component/get?id=cbd6a354-1700-11e8-b451-0a540af4044a&username=personalName&apiKey=a393ceba-1350-11e6-9382-f23c91df2143

我希望原来的jpg图片具有新的名称。

谢谢。

0 个答案:

没有答案