作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用(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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!