【问题标题】:AngularJS: File uploadAngularJS:文件上传
【发布时间】:2017-10-10 13:48:16
【问题描述】:

我是 angularjs 的新手。我的版本是 1.6.4,与 nodeJS 一起使用。我正在按照本教程进行文件上传leon/angular-upload。我完成了此处编写的所有步骤以使其正常工作。

1) bower install --save angular-upload

2) 添加我所做的模块依赖“lr.upload”

angular.module(__appName, ["ngMap", "multipleSelect", "lr.upload"]);

3) 在 html 页面中添加此代码 sn-p,所以我在我的 fileupload.html 中这样做了。

<div class="btn btn-primary btn-upload"
         upload-button
         url="/upload"
         on-success="onSuccess(response)"
         on-error="onError(response)">Upload</div>

4) 最后将脚本添加到 html 页面。

<script src='bower_components/angular-upload/angular-upload.min.js'></script>

但我仍然收到错误:

POST http://localhost:3000/upload 404(未找到)

我的 server.js 代码是:

var express = require("express");
var app = express();

app.use(express.static(__dirname + '/app'));
app.use('/bower_components',  express.static(__dirname + '/bower_components'));

app.get("/", function(req, res){
    res.redirect("app/index.html");
});

app.listen(3000, function(){
    console.log("Tellworld web app listening on port 3000");
})

【问题讨论】:

    标签: angularjs node.js file-upload


    【解决方案1】:

    这是您应该遵循的 Server.JS 文件 https://github.com/leon/angular-upload/blob/master/example/server.js

    你必须添加:

    app.post('/upload', function(req, res) { /*do your post processing here */ })
    

    【讨论】:

      【解决方案2】:

      您的服务器还需要它的路由来接收来自 Angular 的路由指令的请求。用清晰的英语,你需要在你的服务器中声明一个指向上传的路由,它可能看起来像下面的代码

      app.post('/upload', function(req, res){
       console.log(req.body); //this is just to show the request body on your console
      });
      

      【讨论】:

        猜你喜欢
        • 2014-01-01
        • 2020-04-24
        • 2023-03-14
        • 2013-09-05
        • 2013-09-04
        • 2019-02-20
        • 2014-03-14
        相关资源
        最近更新 更多