angularJS从base64生成二进制数据映像并作为映像文件发送到服务器

时间:2022-11-04 20:34:59

I'm using this tool to crop images in my AngularJS app: https://github.com/fengyuanchen/cropper

我正在使用这个工具在我的AngularJS应用程序中裁剪图像:https://github.com/fengyuanchen/cropper

and then I try to store data to server, but now I send base64 code... How could I send normal cropped image binary code, not as base64? is it real?

然后我尝试将数据存储到服务器,但是现在我发送了base64代码…我怎么能发送正常裁剪的图像二进制代码,而不是base64?这是真的吗?

my code for uploading is such:

我上传的代码是这样的:

var addImage = function(files) {
      var deferred = $q.defer();
      var fd = new FormData();      

      var blob = new Blob([files], { type : "image/png"});
      fd.append("file", blob);

      $http.post(settings.apiBaseUri + "/files", fd, {
          withCredentials: true,
          headers: {
            'Content-Type': undefined
          },
          transformRequest: angular.identity
        })
        .success(function(data, status, headers, config) {
          url = data.Url;
          deferred.resolve(url);
        })
        .error(function(err, status) {

        });
      return deferred.promise;
    };

and here I send base64 code:

这里我发送了base64代码:

  $scope.photoChanged = function(files) {
    $scope.oldImg = angular.element(document.querySelector('#avatar-id'));
    $scope.files = files;        
    var reader = new FileReader();
    reader.onload = function(e) {
      $scope.imagecontent = e.target.result;
    };
    reader.readAsDataURL(files[0]);
  };

and

  $scope.setCroppedData = function(data) {
    $('#avatar-id').attr('src', data);
    $scope.nImg = new Image();
    $scope.nImg.src = data;  // data - is base64 image code
  }
  ...
  uploaderService.addImage($scope.nImg.src).then(function(url) {
    $scope.person.AvatarUrl = url;
    $scope.updateContactQuery();
  });
  ...

how could I send like I do without any croping: send image file, so that i could go via link in browser and see image, but not a byte64 code???

我怎么能像以前那样发送:发送图像文件,这样我就可以通过浏览器中的链接看到图像,而不是byte64代码?

1 个解决方案

#1


2  

You can wrap the base64 data in ArrayBuffer then convert to binary Image data like:

您可以将base64数据封装到ArrayBuffer中,然后转换成二进制图像数据,比如:

var binaryImg = atob(base64Image);
var length = binaryImg.length;
var arrayBuffer = new ArrayBuffer(length);
var uintArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < length; i++) {
    uintArray[i] = binaryImg.charCodeAt(i);
}

I suppose uintArray is what you want.

我想uintArray就是你想要的。

#1


2  

You can wrap the base64 data in ArrayBuffer then convert to binary Image data like:

您可以将base64数据封装到ArrayBuffer中,然后转换成二进制图像数据,比如:

var binaryImg = atob(base64Image);
var length = binaryImg.length;
var arrayBuffer = new ArrayBuffer(length);
var uintArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < length; i++) {
    uintArray[i] = binaryImg.charCodeAt(i);
}

I suppose uintArray is what you want.

我想uintArray就是你想要的。