Angularjs Mongoose图像持久性

时间:2014-01-07 15:25:33

标签: node.js mongodb angularjs mongoose

我正在尝试通过angularjs应用程序中的表单将图像持久化到MongoDb数据库中。

我是否必须上传图像,然后对数据库执行保存操作?有一个工作(鼓舞人心)的例子吗?

这是我的代码:

view.html

<div class="form-group">
<label class="sr-only" for="photo">Photo</label>
<input type="file" class="form-control" id="photo" placeholder="Entrez la photo" ng-model-instant ng-model="profil.photo" multiple onchange="angular.element(this).scope().setFiles(this)">

我的控制器如下:

    $scope.uploadFile = function() {
    var fd = new FormData();
    for (var i in $scope.files) {
        fd.append("uploadedFile", $scope.files[i]);
    }
    var xhr = new XMLHttpRequest();
    // xhr.upload.addEventListener("progress", uploadProgress, false);
    xhr.addEventListener("load", $scope.uploadComplete, false);
    xhr.addEventListener("error", uploadFailed, false);
    // xhr.addEventListener("abort", uploadCanceled, false);
    xhr.open("POST", "/fileupload");
    $scope.progressVisible = true;
    xhr.send(fd);
    $scope.loader = true;
}

$scope.uploadComplete = function(evt) {
    /* This event is raised when the server send back a response */
    $scope.affectSrcValue(angular.fromJson(evt.target.responseText));
}

function uploadFailed(evt) {
    console.log("There was an error attempting to upload the file.")
}

$scope.affectSrcValue = function(srcs) {
    $scope.files = [];
    $scope.loader = false;
    $scope.$apply();
}

总结我只需要将我的图像从我的表单存储到MongoDB中,而不需要上传它! 任何人都可以帮助图像的dao持久性?进入base64?

0 个答案:

没有答案