【问题标题】:Angular.js upload image by copy and pasteAngular.js 通过复制粘贴上传图片
【发布时间】:2015-08-02 15:11:17
【问题描述】:

我正在使用这个照片上传指令来上传图片:

https://github.com/danialfarid/ng-file-upload

http://jsfiddle.net/ew4jakn5/

 $scope.upload = function (files) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                Upload.upload({
                    url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                    fields: {
                        'username': $scope.username
                    },
                    file: file
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    $scope.log = 'progress: ' + progressPercentage + '% ' +
                                evt.config.file.name + '\n' + $scope.log;
                }).success(function (data, status, headers, config) {
                    $timeout(function() {
                        $scope.log = 'file: ' + config.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                    });
                });
            }
        }
    };

我需要添加从剪贴板“粘贴”图像的功能,并通过 ng-file-upload 上传。

谁能指点我如何做到这一点?

【问题讨论】:

标签: javascript angularjs browser


【解决方案1】:
 function handlePaste(e) {
            $log.debug(e.originalEvent.clipboardData);
            for (var i = 0 ; i < e.originalEvent.clipboardData.items.length ; i++) {
                var item = e.originalEvent.clipboardData.items[i];
                console.log("Item type: " + item.type);
                if (item.type.indexOf("image") != -1) {
                    $log.debug( item.getAsFile(), {});
                    Upload.upload({file: item.getAsFile(),
                        url: '/upload/', //upload.php script, node.js route, or servlet url
                        // method: POST or PUT,
                        // headers: {'header-key': 'header-value'},
                        // withCredentials: true,
                        data: {user_uuid: $scope.session.user.user_uuid}});
                } else {
                    alert("Discarding non-image paste data");
                }
            }
        }

【讨论】:

    【解决方案2】:

    粘贴图片与删除图片相同

    <div class="img-dropper" ngf-drop="upload($files)" ng-model="context[terminatedkey]" ngf-drag-over-class="'dragover'"  tabindex="0">
        <span>Paste or drop files here</span>
    </div>
    

    默认的$scope.upload = function (file) 会起作用

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 1970-01-01
      • 2021-09-09
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多