首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以jpeg/jpg作为blob填充ng-flow

以jpeg/jpg作为blob填充ng-flow
EN

Stack Overflow用户
提问于 2015-01-31 17:24:05
回答 3查看 1.8K关注 0票数 0

我目前正在尝试用来自我的webserver(nodejs http-server)的图像填充ng流,我找到了以下线程:

使用json中的ng- from .json填充图像文件

从Aidas的回答中可以看到,他说您需要将数据添加到blob中,然后使用addFile(blob)

但是..。当我使用$resource获取url时,如下所示:

http://localhost:8080/3c6397ff-cbb4-4a1c-98b3-5304e02c1bd4.jpg

然后取来自$resource的值,并将其添加到blob中--在ng流中--它表示blob为15 it,而不是实际图像的700 it。

我读过这张图片可能是base64格式的,但是我的google控制台说:

代码语言:javascript
复制
content-length:780831
content-type:image/jpeg; charset=utf-8

如果我看一下google控制台中的响应数据,就会发现大量的问号(它不能显示我猜的字符)。

如何正确格式化响应,以便使用addFile(blob)函数将其添加到ng流中?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-04 10:06:19

最后我就这样做了..。

我的$resource函数如下所示:

ENV.imagesEndpoint = 'http://localhost:8080/

id =图像名称/ ID

代码语言:javascript
复制
getimages: $resource(ENV.imagesEndpoint + id, {}, {
    get: { 
      method: 'GET',  
      isArray: false,
      responseType: 'arraybuffer',
      transformResponse: function(data) {
        // Stores the ArrayBuffer object in a property called "data"
        return { data: data };
      }
      //headers: {'Content-Type': 'image/jpeg;charset=utf-8;base64'}
    }
  })

我的控制器看起来像:

代码语言:javascript
复制
angular.forEach($scope.images, function(imageid) {
        filefactory(imageid).getimages.get().$promise.then(function(value) {
          $timeout(function() {
              var blob = new Blob([value.data], {type: 'image/jpeg'});
              blob.name = 'file.jpg';
              $scope.obj.flow.addFile(blob);
          });
        });
      });
票数 1
EN

Stack Overflow用户

发布于 2015-09-28 11:11:26

我在使用json中的ng- from .json填充图像文件中把cvjensen解决方案和penner的解决方案结合起来

主计长:

首先,我从db读取图像并将它们存储在$scope.project.images中:

代码语言:javascript
复制
$scope.project.images : [ 
        {
            "image_type" : "image/jpeg",
            "src" : "/images/uploaded/flow-122256-fontiosojpg.1",
            "alt" : "fontioso.jpg",
            "_id" : ObjectId("5608ef37f7672d8b1fcab111")
        }
    ]

然后:

代码语言:javascript
复制
Flow.prototype.addExistingFile = function (file, event) {
  var f = new Flow.FlowFile(this, file);
  this.files.push(f);
};

angular.forEach($scope.project.images, function(value, key) {
    getBase64Images.get(value.src) //src contains the full path to img
        .success(function(data) {
             var blob = new Blob([data.data], {type: value.image_type});
              blob.name = value.alt;
              blob.image_url = value.src;
              blob.alt_text = value.alt;
              $scope.uploader.flow.addExistingFile(blob);
        })
        .error(function(error){
            console.log(error);
    });

});

服务:

代码语言:javascript
复制
.factory("getBase64Images", ['$http', function ($http) {
    return {
        get: function (url) {
            return $http.get(
                url, { 
                responseType: 'arraybuffer',
                transformResponse: function(data) {
                  console.log(data);
                  return { data: data };
                }
            }
          );
        }
    };
  }]);
票数 3
EN

Stack Overflow用户

发布于 2016-09-27 19:15:54

我在这里提供的解决方案是基于下面的帖子:

从base64字符串在JavaScript中创建Blob

我遇到了类似的情况,但这些图像使用Base64存储在服务器上。当加载网页时,从数据库检索图像时,必须将这些图像添加回flow.files数组。图像使用Base64字符串保存在数据库中。因此,在页面加载期间,我唯一的方法是将Base64字符串转换为Blob,并将文件添加回flow.files数组。

这使流控制器在从数据库加载页面后能够正确工作。

以下是以下步骤:

  1. 添加指令load-photo并将其添加到输入元素additional_image1中,该元素具有使用jQuery从数据库加载的Base64字符串。
  2. 添加一个指令来访问元素,并在文档上调用作用域函数$scope.loadPhoto,以便加载照片。
  3. 在中,将Base64转换为Blob并将文件添加到flow控件中。
  4. 确保范围变量$scope.imageStringB64和输入元素additional_image1是手动同步的,因为ng-model没有按预期工作。这是因为jQuery外部的代码正在从数据库加载输入元素,我发现它们不是动态绑定的。

JavaScript代码:

代码语言:javascript
复制
app.directive('loadPhoto', function () {
    return function (scope, element, attrs) {
        //This directive 'load-photo' is required to access the DOM element inside the scope
        //This will get the Base64 string of the image which is stored in the input element
        angular.element(document).ready(function () {
            scope.loadPhoto(element[0]);
        })
    }
})

app.controller('photosController', ['$scope', '$http', '$timeout',
    function ($scope, $http, $timeout) {
        ...
        var BLANK_IMG_URL = "//:0";
        $scope.removeFile = function () {
            //debugger;
            $scope.$flow.cancel();
            $scope.imageStringB64 = '';
            $scope.imageString = BLANK_IMG_URL;
        }
        $scope.loadPhoto = function (elem) {
            //Load photo from Database
            //The photo Base64 is stored in the input element 'elem'
            var blobImage;
            var tmpBase64;
            tmpBase64 = angular.element(elem).val(); 
            if (tmpBase64) {
                //Convert Base64 to Blob object
                blobImage = b64toBlob(tmpBase64, 'image/png');
                blobImage.name = "image.png";
                //Add the Blob object to flow files.
                $scope.$flow.addFile(blobImage);
            }
        }
        ...
}]);

HTML代码:

代码语言:javascript
复制
                    <div class="photo-wrapper"  ng-controller="photosController" 
                        flow-init
                        flow-file-added="!isAppraiserSigned() && !!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"
                        flow-files-submitted="$flow.upload()">
                        <h4 class='photo-title'>Photo 1</h4>
                        <div class="property-photo drag-drop-photo" ng-hide="$flow.files.length" flow-drop
                            flow-drag-enter="isAppraiserSigned()?style={}:style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
                            <div class='drag-drop-lbl'>Drop file here</div>
                        </div>
                        <div class="property-photo" flow-drop ng-show="$flow.files.length" 
                            flow-drag-enter="isAppraiserSigned()?style={}:style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
                            <img id="additional_image1_section" style="max-height:100%" flow-img="$flow.files[0]" />
                            <input id="additional_image1" name = "additional_image1" ng-hide="true" type="text" ng-model="imageStringB64" load-photo/>                      
                        </div>
                        <div>
                            <a href="#" class="btn" ng-hide="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}"><%=selectImageLbl%></a>
                            <a href="#" class="btn" ng-show="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Change</a>
                            <a href="#" class="btn btn-danger" ng-show="$flow.files.length"
                               ng-click="removeFile()">
                              Remove
                            </a>
                        </div>
                        <div class='photo-description'>
                            <label class='description-lbl' for='additional_image_describe1'><%=descriptionLbl%></label>
                            <input class='description' id='additional_image_describe1' name='additional_image_describe1' type="text" />
                        </div>
                    </div>

有关将Base64映像转换为blob并返回到Base64的更多选项,请参见此代码示例:

fiddle.jshell.ne

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28254375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档