最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I pass an uploaded CSV file, into my function? - Stack Overflow

programmeradmin1浏览0评论

first of all, pardon me if this is not the correct way of asking a question but this is my first post.

I am working on a university project and am new to JavaScript (and subsequently, jQuery and NodeJS). I am trying to make a system that will take a CSV file, process it and load the results into a database ready to be used by the rest of the system.

I have made it so that I can upload the file to a directory and have a function to process the CSV file into a JSON object (we have only just started to cover databases so I will have to modify this to store in the database), but the problem I'm currently facing is this...

How do I pass/read the uploaded file into the function for processing?

I have searched around and have found similar questions, but these are either usually using PHP or cover different aspects of my issue, without actually touching upon my problem.

I'm sorry if this is something very basic that I am missing, but I'm pulling my hair out over this and can't progress to the rest of the tasks until i have the first part in place - upload, process and store the CSV file.

I will take any advice, either directly related to the issue or pointers from experience that you think I may face, and thank you for you time.

Upload

var express = require("express");
var multer = require("multer");
var fs = require("fs");
var path = require("path");
var app = express();
var upload = multer({ dest: './uploads/'});

app.use(express.static('public'));
app.use(upload);

app.post('/api/csvUpload',function(req,res) {
    upload(req,res,function(err) {
//    console.log(req.body);
//    console.log(req.files);
        if(err) {
            return res.end("Error uploading file");
        }
        else{   
            res.send("File is uploaded");
        }
    });
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("working on port 8081!");
});
<!DOCTYPE html>
<html>
    <head>
        <title>Home Page</title>
    </head>
    
    <body>
        
        <form id="uploadForm" action="/api/csvUpload" method="post" enctype="multipart/form-data">
            <input type="file" name="csvFile">
            <input type="submit" value="Upload File" />
        </form>
        
    </body>
</html>

first of all, pardon me if this is not the correct way of asking a question but this is my first post.

I am working on a university project and am new to JavaScript (and subsequently, jQuery and NodeJS). I am trying to make a system that will take a CSV file, process it and load the results into a database ready to be used by the rest of the system.

I have made it so that I can upload the file to a directory and have a function to process the CSV file into a JSON object (we have only just started to cover databases so I will have to modify this to store in the database), but the problem I'm currently facing is this...

How do I pass/read the uploaded file into the function for processing?

I have searched around and have found similar questions, but these are either usually using PHP or cover different aspects of my issue, without actually touching upon my problem.

I'm sorry if this is something very basic that I am missing, but I'm pulling my hair out over this and can't progress to the rest of the tasks until i have the first part in place - upload, process and store the CSV file.

I will take any advice, either directly related to the issue or pointers from experience that you think I may face, and thank you for you time.

Upload

var express = require("express");
var multer = require("multer");
var fs = require("fs");
var path = require("path");
var app = express();
var upload = multer({ dest: './uploads/'});

app.use(express.static('public'));
app.use(upload);

app.post('/api/csvUpload',function(req,res) {
    upload(req,res,function(err) {
//    console.log(req.body);
//    console.log(req.files);
        if(err) {
            return res.end("Error uploading file");
        }
        else{   
            res.send("File is uploaded");
        }
    });
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("working on port 8081!");
});
<!DOCTYPE html>
<html>
    <head>
        <title>Home Page</title>
    </head>
    
    <body>
        
        <form id="uploadForm" action="/api/csvUpload" method="post" enctype="multipart/form-data">
            <input type="file" name="csvFile">
            <input type="submit" value="Upload File" />
        </form>
        
    </body>
</html>

CSV to JSON

//This will process and convert a CSV file in to a JSON objec 
function csvJson(csv) {
    var lines = csv.split("\n");
    var result = [];
    var headers = lines[0].split(",");
    
    for(var i = 1; i < lines.length; i++) {
        var obj = {};
        var currentLine = lines[i].split(",");
        
        for(var j = 0; j < headers.length; j++) {
            obj[headers[j]] = currentLine[j];
        }
        result.push(obj);
    }
    return JSON.stringify(result);
};

//Below is for testing purposes
var testCSV = "userName, firstName, lastName, studentID, course\n" +
    "username1,first1,last1,123456,Software Engineering\n" +
    "username2,first2,last2,234567,Software Engineering\n" +
    "username3,first3,last3,345678,Software Engineering\n" +
    "username4,first4,last4,456789,Software Engineering";

var processedCSV = csvJson(testCSV);
console.log(processedCSV);

Share Improve this question edited Nov 15, 2015 at 12:05 Richard KeMiKaL GeNeRaL Denton asked Nov 14, 2015 at 14:38 Richard KeMiKaL GeNeRaL DentonRichard KeMiKaL GeNeRaL Denton 4217 silver badges20 bronze badges 3
  • Hi Richard, Wele to Stackoverflow. You have explained your question very well, considering this is your first question. If you could post the code what you have done so far it would be easier for other to figure it out – WoodChopper Commented Nov 14, 2015 at 14:47
  • So the upload part is working, and you want a function to read the file and save its contents into a database? Presumably, as you mention Javascript, this function is expected to run within a web page? The problem is, Javascript is not going to be able to municate with a database. It can send requests and data to a web server, and read the web server's response, but to do anything with databases you will need a server-side script using a more powerful language like PHP / Perl / Ruby etc. Your Javascript would then send the data to that script. – Doug McLean Commented Nov 14, 2015 at 15:01
  • Thank you for your feedback, I have added the code as requested. – Richard KeMiKaL GeNeRaL Denton Commented Nov 15, 2015 at 12:06
Add a ment  | 

1 Answer 1

Reset to default 3

Because it's not pletely clear for me how the files are uploaded, I'll start with the functions that gives the ability to load a (CSV)-file.

For loading a file or url you can use XMLHttpRequest, more on this can be found at W3Schools. This is a really powerful object, and I would really advice to read more into this.

 /**
 * Loads file using XMLHttpRequest
 * @param {String} path
 * @param {Function} success
 * @param {Function} error
 */
function loadFile(path, success, error) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
                    success(csvToJSON(xhr.responseText));
            } else {
                if (error)
                    error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}

To use this function, simply execute the following code:

loadFile('Link_to_CSV_file.csv', function (data) {
        myFunction(data);
    }, function (xhr) {
        console.error(xhr);
    });

As soon the data is succesfully loaded, myFunction() will be executed with the data from loadFile(). This can be used in the function.

Finally, load the CSV-data in a function:

function myFunction(data){
    console.log(data);
}

PS: I would advice to convert the CSV file to JSON if you want to handle it in a javascript-function, if you're using Node.js, check out How to convert CSV to JSON in Node.js.

发布评论

评论列表(0)

  1. 暂无评论