Thank you. Here's the thing. I'm learning nodejs and I'm having a problem with image uploading.
<input type="file" name="file" id="file">
<button id="btn">上传</button>
<img src="" alt="" id="img">
<script>
let file = document.querySelector('#file')
let btn = document.querySelector('#btn')
let img = document.querySelector('#img')
btn.addEventListener('click',function(){
console.log(file.files[0]);
let files_data = file.files[0]
let fileReader = new FileReader()
fileReader.readAsDataURL(files_data)
let base64;
let date = new Date()
let time = date.toJSON()
fileReader.onloadend = function(e){
base64 = e.target.result
console.log('base641',base64);
img.src = e.target.result
console.log(typeof e.target.result);
let xml = new XMLHttpRequest()
xml.open('POST','http://localhost:3000/api/swiper',true)
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.onload = function(){
}
console.log('base64',base64);
let obj = {
swi_url:base64,
swi_qiyong:false,
swi_type:'活动',
swi_time:time
}
xml.send(JSON.stringify(obj))
}
})
nodejs code:
const express = require('express')
const router = express.Router()
const multer = require('multer')
const mysql = require('../model/index')
const path = require('path')
const dizhi = path.join(__dirname,'../upload')
router.post('/',async(req,res) => {
const data = req.body
console.log(data);
})
module.exports = router
I converted the image into base64 format data in the front-end code, and then passed it to the back end through post request. I also received it through req.body, but it could not point to one of the data, and it would give an error if I pointed to it, and it could not point to the body parameter data of json.parent. I would like to ask you where the code is wrong.
Just started learning nodejs, asking really good questions baici, thanks for understanding
0 Answer
No answer yet
这家伙很懒,什么都没留下...