【问题标题】:angularjs $http response data as readStream to Aliyun OSS?angularjs $http 响应数据作为 readStream 到阿里云 OSS?
【发布时间】:2017-02-16 16:29:48
【问题描述】:

我想使用angularJS提供的$http服务通过XHR下载一个图片文件,并将响应数据(图片数据)上传到OSS(这是阿里巴巴提供的托管文件的服务),它是@ 987654321@是:

表示它将把{String|Buffer|ReadStream}作为第二个参数file, 但是如何传输响应数据,以便我可以将其作为 put(name, file) 方法的参数,例如:

$http.get("http://image.url/file.gif").then(
    function success(response){
        console.log("type of response.data is :" + typeof response.data);
        oss.put("test.jpg", response.data); //<-- here will give an error
    },
    function fail(response){
       console.log("error");
    }
)  

这会产生类型错误:

任何建议或答案表示赞赏。

【问题讨论】:

    标签: angularjs xmlhttprequest


    【解决方案1】:

    如果您一直通过链接获取图像并且想要从中获取字符串数据,我尝试了一个小功能,希望对您有所帮助,它会获取链接并将图像提供给您base64字符串格式的数据

    function getBase64StringFromImgLink(imageSrc, imgType) {
    
      var imageAsCanvas;
    
      /*
       * Creates a new image object from the src
       * Uses the deferred pattern
       */
      var createImage = function () {
          var deferred = $.Deferred();
          var img = new Image();
    
          img.onload = function() {
              deferred.resolve(img);
          };
          img.src = imageSrc;
          return deferred.promise();
      };
    
      /*
       * Create an Image, when loaded and start to resize
       */
      $.when(createImage()).then(function (image) {
            imageAsCanvas = document.createElement("canvas");
            imageAsCanvas.width = image.width;
            imageAsCanvas.height = image.height;
            var ctx = imageAsCanvas.getContext("2d");
            ctx.drawImage(image, 0, 0, imageAsCanvas.width, imageAsCanvas.height);
    
            $scope.$apply(function($scope){
              $scope.imageAsBase64String = imageAsCanvas.toDataURL(imgType);
              oss.put("test.jpg", $scope.imageAsBase64String); //<-- here will be the data string i hope it works
            });
        }, function () {console.log('error creating an image')});
    }
    //How to call example
    getBase64StringFromImgLink('images/george-avatar.jpg', 'image/jpeg');
    
    <!--HTML -->
    <img data-ng-src="{{imageAsBase64String}}" >
    

    【讨论】:

    • 感谢您的回答,不幸的是,我要获取和上传的图像是gif,在这种情况下,从画布中检索数据似乎无济于事,不过谢谢 :-)跨度>
    • np,抱歉没有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2018-05-18
    • 2022-01-17
    • 2018-08-23
    • 2019-04-03
    • 2019-06-21
    • 2020-01-13
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多