gpt4 book ai didi

android - 将图像上传到 Node.js 意外 token

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:09 25 4
gpt4 key购买 nike

我想从android上传图片到node.js,关注this one ,我现在可以从网络查看图像,但始终上传图像

SyntaxError: Unexpected token �

如果我选择 1.5mb 文件,它会显示

Error: request entity too large

我找到了this ,看来我必须将文件转换为 JSON

但我虽然已经使用这三个来加载大文件并将其转换为 JSON

app.use(bodyParser.json());
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true }))

卡在这里好几天了,请帮忙

代码

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http');
app.use(bodyParser.json());
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies.
songs = require('./routes/route');
var jsonParser = bodyParser.json();
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
app.get('/', function (req, res) {
res.send('Hello World!');
});

app.get('/songs',songs.findAll);
app.get('/findById/:id',songs.findById);
app.post('/songs',songs.addSong);
app.put('/songs/:id',songs.updateSong);
app.delete('/songs/:id',songs.deleteSong);
app.post('/upload',songs.updateSong);
app.get('/upload/:file',songs.viewByFile);

路线代码

exports.upload = function(req, res) {
console.log(req.files.image.originalFilename);
console.log(req.files.image.path);
fs.readFile(req.files.image.path, function (err, data){
var dirname = "/Node/file-upload";
var newPath = dirname + "/uploads/" + req.files.image.originalFilename;
fs.writeFile(newPath, data, function (err) {
if(err){
res.json({'response':"Error"});
}else {
res.json({'response':"Saved"});
}
});
});
};

错误

SyntaxError: Unexpected token �
at parse (c:\Users\awei\node_modules\body-parser\lib\types\json.js:83:15)
at c:\Users\awei\node_modules\body-parser\lib\read.js:116:18
at invokeCallback (c:\Users\awei\node_modules\raw-body\index.js:262:16)
at done (c:\Users\awei\node_modules\raw-body\index.js:251:7)
at IncomingMessage.onEnd (c:\Users\awei\node_modules\raw-body\index.js:308:7)
at emitNone (events.js:80:13)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:906:12)
at nextTickCallbackWith2Args (node.js:475:9)
at process._tickCallback (node.js:389:17)

最佳答案

如果您使用像 postman 这样的扩展,请确保请求 header 中的 Content-Type 是 multipart/form-data 而不是 application/json。

关于android - 将图像上传到 Node.js 意外 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35618317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com