使用angularjs和JIC将文件上传到S3

时间:2014-05-14 03:19:41

标签: javascript html angularjs canvas amazon-s3

我有角度上传到S3工作,但我想将图像文件压缩(或缩小)到250 px高度。使用角度上传:

$scope.onFileSelect = function($files) {
        $scope.files = $files;
            $scope.upload = [];
            for (var i = 0; i < $files.length; i++) {
                var file = $files[i];
                console.log('file: ' + JSON.stringify(file));
                file.progress = parseInt(0);

...

然后进行上传处理。这很有效...除了尺寸问题。所以,我最终尝试使用一个名为JIC(https://github.com/brunobar79/J-I-C)的库来处理压缩。我的console.log(文件)的结果是:

file: {"webkitRelativePath":"","lastModifiedDate":"2014-04-25T19:35:22.000Z","name":"pooltable.jpg","type":"image/jpeg","size":1922990} 

我尝试将此文件传递给压缩,然后继续上传到S3,但它不起作用:

var file = compress($files[i], 50, $files[i].type);

这是来自JIC的压缩代码,我修改了它以满足我的高度要求:

var compress = function(source_img_obj, quality, output_format){

        var mime_type = "image/jpeg";
        if(output_format != undefined && output_format == "png"){
            mime_type = "image/png";
        }
        var cvs = document.createElement('canvas');
        var ratio = 250 / source_img_obj.naturalHeight;
        cvs.width = ratio * source_img_obj.naturalWidth;
        cvs.height = 250;
        var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
        var newImageData = cvs.toDataURL(mime_type, ratio/100);
        var result_image_obj = new Image();
        result_image_obj.src = newImageData;
        return result_image_obj;
    };

这里的问题是我的source_img_obj不是来自HTML,而是来自用户从本地机器上传图像,因此它没有src属性,我收到此错误:

Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.

我实际上并不想将图像绘制到我的html文件中,我只想缩小它们并将它们直接上传到服务器。

0 个答案:

没有答案