-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
57 lines (56 loc) · 1.93 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const request = require('request');
const path = require('path');
const fs= require('fs');
const moment= require('moment');
async function getMediaid(files,content){
/**
*上传本地
*/
console.log(files)
const file = files.file;
const allow = ["jpg", "png", "jpeg","gif"];
let ext = file.name.split('.')[1]
if(allow.indexOf(ext)<0){
//super.APIErr('1004', "上传格式不被允许")
}
// let reader = fs.createReadStream(file.path);
// let newFilename = moment().format("X") + '.' + file.name.split('.')[1];
// //先上传图⽚到本机服务器
// const upStream = fs.createWriteStream(path.join(__dirname+`/public/media`, newFilename));
// reader.pipe(upStream);
// let file_path = path.resolve(__dirname, "public/media/"+newFilename);
let file_path = file.path
//let accesstoken = await this.getAccessToken();
let api = 'http://api.weixin.qq.com/cgi-bin/media/upload?type=image'
return new Promise((resolve, reject) => {
request.post({
url:api,
headers : { 'Content-Type' : 'multipart/form-data' },
formData: {
media: {
value: content,
options: {
filename: file.name,
'content-type': 'image/'+file.name.split('.')[1],
}
}
}
},(err, httpResponse, body) =>{
if (err) {
reject(err)
}
console.log("ok"+body+","+err)
resolve(JSON.parse(body))
});
})
}
async function start(){
let json=await getMediaid({
file:{
name:"1.jpg",
path:"1.jpg"
}
})
console.log("ok"+json)
}
module.exports=getMediaid;