简体   繁体   中英

Change form action to controller action

I want upload files in my server using Angular-Node-Express. Actually I have this form

<html>
    <body>
        <form ref='uploadForm' id='uploadForm' action='/api/upload' method='post' encType="multipart/form-data">
                <input type="file" name="sampleFile" />
                <input type='submit' value='Upload!' />
        </form>     
    </body>
</html>

And in my server I have this

var express = require('express');
var fileUpload = require('express-fileupload');
var app = express();

// default options 
app.use(fileUpload());

app.post('/api/upload', function(req, res) {
    var sampleFile;

    if (!req.files) {
        res.send('No files were uploaded.');
        return;
    }

    sampleFile = req.files.sampleFile;
    sampleFile.mv('./uploads/'+ Date.now()+'.jpg', function(err) {
        if (err) {
            res.status(500).send(err);
        }
        else {
            res.send('File uploaded!');
        }
    });
});

I want to do other process in my controller before to send the file like add names, or check values that I have in this scope

function upload($scope, $http,$state){
    //Some process
    $http.post('/upload','myFile')
        .success(function(data){
            //More process
        });
};

But I dont know hot to catch the file in my controller

I suggest the usage of ngFileUpload .

I use it like this:

html:

<form ng-submit="vm.submit()" enctype="multipart/form-data">
    <input type="file" 
      ngf-select="" 
      ng-model="vm.uploads.file" 
      ngf-multiple="true" 
      ngf-change="vm.fileSelected(vm.file, $event)" />
</form>

js:

function fileSelected(files, $event) {
    vm.files // here you can access 1 or an array of files
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM