gpt4 book ai didi

angularjs - 使用ngImgCrop上传 Angular 文件

转载 作者:行者123 更新时间:2023-12-04 13:50:00 24 4
gpt4 key购买 nike

我正在使用(ngImgCrop)裁剪图像,然后使用(angular-file-upload)将裁剪后的图像上传到服务器。

我可以从ngImgCrop的“on-change”选项中获取$ dataURI。但是我需要一个文件实例来调用$ upload。
我如何获取裁剪后的图像的File实例以便上传:

        $scope.upload = $upload.upload({
url: '/api/fileupload',
file: [**file cropped here**]
}).progress(function (evt) {
//
}).success(function (data, status, headers, config) {
//
});

最佳答案

我想您会在这种方法中找到正确的答案。我在Github的angular-file-upload问题页面(https://github.com/nervgh/angular-file-upload/issues/208)中找到了它:

/**
* Converts data uri to Blob. Necessary for uploading.
* @see
* http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata
* @param {String} dataURI
* @return {Blob}
*/
var dataURItoBlob = function(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: mimeString});
};

您应该能够使文件实例执行以下操作:
var blob = dataURItoBlob($scope.croppedImage);

我不知道它是否运作良好,但似乎。

关于angularjs - 使用ngImgCrop上传 Angular 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25981813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com