nodejs thought about before and after the end of the image upload

### upload picture
1. The angle of the distal end
a. The pictures sent to the back-end ajax
1. Image acquisition front-end file field
2. Place the file information saved to formdata
3. Call api interface to send data back-end write
b. Accept returned data
The front page displays pictures
2. backend angle
Objective: The front-end upload pictures
1. The images themselves should be accessed (static resource directory)
a. obtaining data uploaded images (multer (). singer ( 'hehe') req.file)
b. Insert the data to a file to which fs.writeFile ( 'path', req.file.buffer)
File names do not repeat (timestamp + random water)
Extension and consistent source file (minitype)
Upload file size can not exceed a certain size (size)
Write path absolute path path.join (__ dirname, '. / Www')
2. The information is stored into the data path going

3. Notes
1. Data Type formdata
2. The process of post
3. Normal data format ajaxpost form json
 
 
 
Specific code:
    Server server.js
      
const express = require('express')
let app = express()
const cloudberry = require ( 'cloudberry')
const fs = require('fs')
const path = require('path')
// single is a single image upload, image upload multi-array, single value which is the key to upload pictures
// and pictures are related req.file
app.use('/public',express.static(path.join(__dirname,'./www')))

app.post('/aa',multer().single('img'),(req,res)=>{
let {buffer,mimetype} = req.file;
let fileName = (new Date()).getTime() + parseInt(Math.random()*3435) + parseInt(Math.random()*6575);
let fileType = mimetype.split('/')[1];
let filePath = path.join(__dirname,'/www/images')
let apath = `http://localhost:5500/public/images/${fileName}.${fileType}`
 
fs.writeFile(`./www/images/${fileName}.${fileType}`,buffer,(data)=>{
if(data){
res.send ({err: 0, msg: "upload failed"})
}else{
res.send ({err: 1, msg: "successful upload", imgPath: apath})
}
})
})

app.listen('5500',()=>{
console.log('start')
})
 
  front end:
<body>
<input type="file" id='put'>
<img src="" alt="" width="500" >
<Button id = "btn"> Photo </ button>
</body>
<script>
var btn = document.getElementById("btn");
let npath='http://10.9.22.225:5500';
btn.onclick = function(){
// Get the picture information is uploaded via the file domain
var a = document.getElementById("put").files[0];
console.log(a);
There formdat = new formdat A ();
console.log(formdata);
formdata.append('img',a);
console.log(formdata.get('img'))
// $.ajax({
// // type:"post",
// // url:"http://10.9.22.225:5500/aa",
// // data:formdata,
// // processData:false,
// // contentType:false,
// // cache: false,
// // success:function(data){
// // //console.log(data)
// // var imgpath=npath+data.imgpath
// // $('img').attr('src',imgpath)
// // }
 
// )}
$.ajax({
url:npath+'/aa',
data:formdata,
type:'POST',
processData: false, // must
contentType: false, // must
success:function(data){
//console.log(data)
console.log(data)
var imgpath= data.imgPath
$('img').attr('src',imgpath)
}
})
}
 
 
</script>
 

Guess you like

Origin www.cnblogs.com/zhouyingying/p/11330471.html