如何使用mean.js上传图片并保存到数据库(猫鼬)

问题描述 投票:1回答:1

我是新来的平均堆栈。我想知道如何将图像文件上传到数据库(猫鼬)通过AngularJS。请,如果你能为我提供一些代码。我已经在网上搜索,我发现这个代码,但它不工作对我来说

HTML代码:

<div class="col-md-4 col-xs-8">
    <div class="fileinput fileinput-new" data-provides="fileinput">
        <img class="profile-pic thumbnail" style="width: 200px; height: 150px;" />
        <div>
            <span class="btn btn-default btn-file">
            <span class="fileinput-new upload-button"><span class="glyphicon glyphicon-upload"></span> Upload New Pic</span>
            <input class="file-upload" type="file" data-ng-file-select="onFileSelect($files)"  accept="image/png, image/jpeg" >
            </span>                 
        </div>

        <span data-ng-if="uploadInProgress">Upload progress: {{ uploadProgress }}</span>
        <img data-ng-src="uploadedImage" data-ng-if="uploadedImage">
        <br>
    </div>
</div>

客户端controller.js:

$scope.onFileSelect = function(image) {
    console.log("image selected" + image);
    if (angular.isArray(image)) {
        image = image[0];
    }

    // This is how I handle file types in client side
    if (image.type !== 'image/png' && image.type !== 'image/jpeg') {
        alert('Only PNG and JPEG are accepted.');
        return;
    }

    $scope.uploadInProgress = true;
    $scope.uploadProgress = 0;

    $scope.upload = $upload.upload({
        url: '/upload/image',
        method: 'POST',
        file: image
    }).progress(function(event) {
        $scope.uploadProgress = Math.floor(event.loaded / event.total);
        $scope.$apply();
    }).success(function(data, status, headers, config) {
        $scope.uploadInProgress = false;
        // If you need uploaded file immediately 
        $scope.uploadedImage = JSON.parse(data);      
    }).error(function(err) {
        $scope.uploadInProgress = false;
        console.log('Error uploading file: ' + err.message || err);
    });
};

即使选择文件控制器没有触发后,我的问题是。我也保持一个控制台来测试,但它不工作。

javascript angularjs meanjs
1个回答
0
投票

这是我类似的问题,从而节省了MongoDB中的服务器,并把URL /名称上的文件的响应。 https://stackoverflow.com/a/28148042/4494320 它需要multer(NPM安装multer --save)。

© www.soinside.com 2019 - 2024. All rights reserved.