简体   繁体   中英

Extracting uploaded csv data with multer

I am porting a rails app over to use the MEEN stack (Mongo, Express, Ember, Node)

I have a function that takes an uploaded csv and extracts the data from it and uses the data to then form SQL queries to a database. For some reason I am having issues with accessing the uploaded csv data with multer.

My Router file

var quotes = require('../api/quote');
var cors = require('cors');
var sku = require('../api/tools/sku');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var util = require("util");
var fs = require("fs"); 

var corsOptions = {
    origin: 'http://localhost:4200'
}

module.exports = function(router){
    router.route('/quotes').post(cors(corsOptions),function(req,res){
        console.log(req.body);
        quotes.addQuote(req,res);
    }).get(cors(corsOptions),function(req,res){
        quotes.getAllQuotes(req,res);
    });
    router.route('*').get(cors(corsOptions), function(req,res){
        res.sendFile('public/index.html',{root:'./'});
    });
    router.post('/tools/sku/reactivate',upload.single('csvdata'),function(req,res){
        console.log(req.files);
        console.log('handing request over to sku.reactivate');
        sku.reactivate(req,res);
    });
};

My handlebars file for the tools/sku/reactivate template

<div class="col-md-8 col-md-offset-2 text-center">
  <h2 class="toolTitle">Reactivate SKUs</h2>
  <p class="lead">CSV Should Contain 1 Column (SKU) Only</p>
  {{file-upload enctype="multipart/form-data" type="file" url="/tools/sku/reactivate" class="center-block" accept="text/csv" name="csvdata"}}
</div>

i am getting Error: Unexpected field when I attempt to post the file upload to the /tools/sku/reactivate post route. I don't understand whats wrong with my code.

The issue was using the file-upload ember addon. As soon as I removed the handlebars component and just hardcoded a form as seen below, the file upload's successfully.

<div class="col-md-8 col-md-offset-2 text-center">
  <h2 class="toolTitle">Reactivate SKUs</h2>
  <p class="lead">CSV Should Contain 1 Column (SKU) Only</p>
  <form action="/tools/sku/reactivate" method="POST" enctype="multipart/form-data">
    <input class="center-block" type="file" name="csvdata">
    <button type="submit" class="btn btn-md btn-danger">Submit</button>
  </form>
</div>

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