MTA
MTA

Reputation: 1073

AngularJS: File upload

I am a newbie of angularjs. My version is 1.6.4 using with nodeJS. I am following this tutorial for file upload leon/angular-upload. I did all the steps written here to make it work which are.

1) bower install --save angular-upload

2) Add module dependency "lr.upload" which i did

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

3) Add this code snippet in html page, so i did in my fileupload.html.

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

4) And finally add script to html page.

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

But i am still getting error:

POST http://localhost:3000/upload 404 (Not Found)

My server.js is code is:

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");
})

Upvotes: 0

Views: 707

Answers (2)

Hassan Sani
Hassan Sani

Reputation: 124

Your server will also need it route that receive request from the angular's route directive. In clear english you will need to declare a route pointing to upload in your server which may look like the code below

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

Upvotes: 1

Kumar Nitesh
Kumar Nitesh

Reputation: 1652

Here is Server.JS files that you should follow https://github.com/leon/angular-upload/blob/master/example/server.js

You have to add :

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

Upvotes: 2

Related Questions