nodejs + multer + angularjs 用于上传而不重定向 [英] nodejs + multer + angularjs for uploading without redirecting

查看:28
本文介绍了nodejs + multer + angularjs 用于上传而不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Nodejs + Multer +angularjs 在服务器上上传文件.

我有一个简单的 HTML 文件:

I am using Nodejs + Multer +angularjs for uploading files on the server.

i have a simple HTML file:

<form action="/multer" method="post" enctype="multipart/form-data">
<input type="file" id="photo" name="photo"/> 
<button id="Button1">Upload</button>
</form>

Nodejs 部分:

var multer  = require('multer');

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})

app.post('/multer', upload.single('photo'), function (req, res) {
    res.end("File uploaded.");
});

完美运行,文件上传成功.

但这在上传文件后将我重定向到/multer"(因为表单元素).
我如何保持在同一页面上??..可能使用 angularjs
所以我尝试了这个:

制作 HTML 角度文件:

this works perfectly and the file is successfully uploaded.

but this redirect me to "/multer" after uploading the file (because of the form element).
How do i stay on the same page??..possibly using angularjs
so i tried this:

making a HTML angular file:

<section data-ng-controller="myCtrl">
<input type="file" id="photo" name="photo"/> 
<button id="Button1" ng-click="f()">Upload</button>
</section>

和一个 Angularjs 控制器:

and a Angularjs controller:

angular.module('users').controller('myCtrl',[$scope,function($scope){
    $scope.f=function(){
        var photo = document.getElementById('photo');
        var file = photo.files[0];
        if (file) {

            //code to make a post request with a file object for uploading?????

            //something like..
            //$http.post('/multer', file).success(function(response) {
            //console.log("success");
            //});
        }
    }
}]);


有人可以帮助我使用来自 ANGULARJS 控制器的 MULTER 上传文件对象的 POST 请求的代码吗?

谢谢

推荐答案

Angularjs 指令:

Angularjs directive:

angular.module('users').directive('fileModel', ['$parse', function ($parse) {
return {
    restrict: 'A',
    link: function(scope, element, attrs) {
        var model = $parse(attrs.fileModel);
        var modelSetter = model.assign;

        element.bind('change', function(){
            scope.$apply(function(){
                modelSetter(scope, element[0].files[0]);
            });
        });
    }
};
}]);

Angular html 文件:

Angular html file:

<input type="file" file-model="myFile"/><br><br>
<button ng-click="uploadFile()">Upload</button>

Angularjs 控制器:

Angularjs Controller:

$scope.uploadFile = function(){

        var file = $scope.myFile;
        var uploadUrl = "/multer";
        var fd = new FormData();
        fd.append('file', file);

        $http.post(uploadUrl,fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(){
          console.log("success!!");
        })
        .error(function(){
          console.log("error!!");
        });
    };

Nodejs 服务器路由文件:

Nodejs server route file:

var multer  = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname+ '-' + Date.now()+'.jpg')
    }
});
var upload = multer({ storage: storage });

app.post('/multer', upload.single('file'));

享受吧!

这篇关于nodejs + multer + angularjs 用于上传而不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆