Javascript 从React本机应用程序向Express api发送CSV文件
我已经为此苦苦挣扎了好几个小时,却找不到正确的解决方案。所以基本上我有一个REST API,它看起来像这样:Javascript 从React本机应用程序向Express api发送CSV文件,javascript,node.js,reactjs,react-native,express,Javascript,Node.js,Reactjs,React Native,Express,我已经为此苦苦挣扎了好几个小时,却找不到正确的解决方案。所以基本上我有一个REST API,它看起来像这样: //multer config const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, ""); }, filename: function (req, file, cb) { cb(null, new Date().v
//multer config
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "");
},
filename: function (req, file, cb) {
cb(null, new Date().valueOf() + "-" + file.originalname);
},
});
const upload = multer({
storage,
limits: {
fileSize: 30 * 1024 * 1024,
},
}).single("list");
app.post("/", upload, function (req, res) {
// console.log(dataURLtoFile(req.body,'list'))
console.log(req.body);
CSVToJSON()
.fromFile(req.file.path)
.then((source) => {
source.forEach((res) => {
firebase
.firestore()
.collection("races")
.add(res)
.then(() => {});
});
})
.then(() => {
fs.unlinkSync(req.file.path);
});
res.send("All good");
console.log(req.body);
});
Object {
"name": "biostats.csv",
"size": 849,
"type": "success",
"uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540yawning%252FChatOneonOne/DocumentPicker/218c8859-4bdb-4af4-ac61-f1c71688cca5.csv",
}
因此,它接收CSV文件,将其保存在本地,将其转换为JSON,将JSON发送到Firebase Firestore并删除临时CSV文件。当前端是一个普通的HTML表单时,它工作得很好,但是当我尝试用expo document picker上传它时,它就不起作用了。我得到的是一个如下所示的对象:
//multer config
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "");
},
filename: function (req, file, cb) {
cb(null, new Date().valueOf() + "-" + file.originalname);
},
});
const upload = multer({
storage,
limits: {
fileSize: 30 * 1024 * 1024,
},
}).single("list");
app.post("/", upload, function (req, res) {
// console.log(dataURLtoFile(req.body,'list'))
console.log(req.body);
CSVToJSON()
.fromFile(req.file.path)
.then((source) => {
source.forEach((res) => {
firebase
.firestore()
.collection("races")
.add(res)
.then(() => {});
});
})
.then(() => {
fs.unlinkSync(req.file.path);
});
res.send("All good");
console.log(req.body);
});
Object {
"name": "biostats.csv",
"size": 849,
"type": "success",
"uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540yawning%252FChatOneonOne/DocumentPicker/218c8859-4bdb-4af4-ac61-f1c71688cca5.csv",
}
我正在做的基本上是尝试将URI部分转换为blob,因为我已经用一个图像完成了这项工作,并且工作正常。以下是我的上传功能:
const uploadImage = async () => {
// Check if any file is selected or not
const fd = new FormData();
if (singleFile != null) {
const toUpload = singleFile
const response = await fetch(toUpload);
const blob = await response.blob();
fd.append("list", blob);
fetch("http://192.168.1.163:5000/", {
method: "POST",
body: fd,
headers: {
"Content-Type": "multipart/form-data",
},
}).then(res => console.log(res)).catch(err => console.log(err.message))
}
};
}
所以我现在非常绝望,不知道如何继续。如果有人知道怎么做,请分享。我欠你一个人情