【发布时间】:2021-02-26 14:30:00
【问题描述】:
Backend code
router.route('http://localhost:5007/api/media')
.post(mediaCtrl.saveMedia)
async saveMedia(req, res) {
let file = req.files.file
let ext = req.body.extension
let path = req.body.path
if(_.isNull(file) || _.isEmpty(file)){
return res.status(400).json({
success:false,
response: null,
message: "El file vacio"
})
}
if(_.isNull(ext) || _.isEmpty(ext)){
return res.status(400).json({
success:false,
response: null,
message: "El campo extension esta vacio"
})
}
if(_.isNull(path) || _.isEmpty(path)){
return res.status(400).json({
success:false,
response: null,
message: "El campo patch esta vacio"
})
}
let fr = new FileReader()
fr.onload = () => {
let fileName = moment().unix().toString() + '_' + file.originalFilename
console.log(fr.result);
let params = {
Bucket: config.s3.bucket,
Body: fr.result,
//ContentEncoding: 'base64',
Key: path + "/" + fileName,
ContentType: file.type,
ACL: 'public-read'
}
s3bucket.putObject(params, async (err, data) => {
if(err) {
return res.json({ success: false, response: err })
}
var url = "https://" + config.s3.bucket + ".s3.amazonaws.com/" + params.Key
console.log('25131',data)
if(data) {
data["url"] = await url
}
return res.json({
success : true,
response : data
})
})
}
fr.onerror = () => {
console.log('read error')
}
fr.readAsArrayBuffer(file)
},
Frontend code
private saveAws() {
const steps: Array<any> = this.structSaveApp.steps;
if (steps[this.step_save] != undefined) {
const options: Array<any> = steps[this.step_save].options;
this.file_list.forEach((file) => {
options.forEach((item) => {
if (file.key === item.key) {
const file_multimedia: File = file.file;
const multimedia: FormData = new FormData();
multimedia.append('file', file_multimedia);
multimedia.append('extension', file_multimedia.type);
multimedia.append('path', this.user_id);
this.spinner.show();
// var object = {};
// multimedia.forEach((value, key) => object[key] = value);
// var json:any = JSON.stringify(object);
// console.log('saveAWS file',object);
this.appsService.saveAWS(multimedia).subscribe((response) => {
console.log(response);
item.value = response.response.url;
this.spinner.hide();
}, (error) => {
console.log(error);
this.spinner.hide();
});
}
});
});
}
}
service
public saveAWS(data: FormData): Observable<any> {
let headers = new HttpHeaders({
});
return this.http.post('http://localhost:5007/api/media', data, { headers: headers });
}
当我尝试从前端上传到后端时,它会给我一个错误,例如 Unexpected token - in JSON at position 0 at JSON.parse () erro 你能请任何人帮我解决这个问题吗.................................................. …………
error:SyntaxError: Unexpected token - 在 JSON 中的位置 0 在 JSON.parse() 在 createStrictSyntaxError (C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\body-parser\lib\types\json.js:158:10) 解析时(C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\body-parser\lib\types\json.js:83:15) 在 C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\body-parser\lib\read.js:121:18 在 invokeCallback (C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\raw-body\index.js:224:16) 完成后(C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\raw-body\index.js:213:7) 在 IncomingMessage.onEnd (C:\Users\THIPPESH_PANI\Downloads\QudraforceBackend\backend\socialtools-api-media\src\node_modules\raw-body\index.js:273:7) 在 IncomingMessage.emit (events.js:327:22) 在 IncomingMessage.EventEmitter.emit (domain.js:483:12) 在 endReadableNT (_stream_readable.js:1220:12) 在 processTicksAndRejections (internal/process/task_queues.js:84:21)
【问题讨论】:
-
这可能是因为你的json语法不正确
-
@Kshitij 可以解释一下..
-
请帮忙
-
@Kshitij 在上面我有前端代码你能检查并告诉我是否发送正确的 json 数据...因为如果我使用邮递员并检查后端工作正常..但是当我在通过前端发送它在后端的解析器错误