因此,使用express-fileupload中间件之间的唯一变化是req.files == null而不是未定义({req.files
是未定义的,当我不使用express-fileupload时,我认为我不需要要使用它吗?)。显然,服务器脚本在excel = req.files.myFile.path;
处停止,因为它无法读取null或undefined的属性myFile
。我在这里很茫然-我很确定这是我的前端脚本存在的问题,但是我对ajax不太了解,并且原生JS和JQuery的组合可能会让我感到困惑?
//Front end:
var App = {};
App.handleFileUpload = function handleFileUpload(event) {
var files = event.target.files;
var myFile = files[0];
that.sendFile(myFile);
}
$('#fileUpload').on('change', function(event) {
App.handleFileUpload(event)
},
sendFile = function sendFile(file) {
var uri = 'http://MYIP/getsheet';
var xhr = new XMLHttpRequest();
var fd = new FormData();
xhr.open('POST', uri, true);
fd.append('myFile', file);
xhr.send(fd);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
var log = xhr.responseText;
makeDivs(JSON.parse(log));
console.log(log);
}
}
});
//Server:
const express = require('express');
var app = express();
const path = require('path');
const ce2j = require('convert-excel-to-json');
const moment = require('moment');
const fu = require('express-fileupload');
app.use(fu());
var log;
app.post('/getsheet', function(req, res){
console.log(req.files);
excel = req.files.myFile.path;
etc...
<body>
<input type ="file" name ="excel" id="fileUpload" />
<div id = "main"></div>
</body>
我尝试过同时设置<form enctype>
和不设置for(let c of data){
let node = document.createElement('li');
node.textContent = c.companyName + ', ' + c.symbol; //only want c.symbol
companyList.appendChild(node);
node.addEventListener('click', function(e){
if(e.target && e.target.nodeName.toLowerCase() == "li"){
populateCompanyInfo(e);
}
});
}
function populateCompanyInfo(e){
//I want to be able to access the c.symbol value in here using the taget
}
,并且没有任何区别(我也不认为这应该吗?)。