安装依赖库
npm install myjs-common
参数
// 待上传文件数组
let files = [
"C:\\Users\\bm\\img\\0.jpg",
"C:\\Users\\bm\\img\\1.jpg",
"C:\\Users\\bm\\img\\2.jpg"
];
// 上传参数选项, 上传url地址=localhost:8888/ai/admin/manage/upload
let options = {
method: 'post',
host: 'localhost',
port: 8888,
path: '/ai/admin/manage/upload',
file: "file"
};
js 调用
http 方式
const FuRequire = require("myjs-common").FuRequire;
FuRequire.http(files, options, (code, data) => {
console.log(`响应码: ${code}`);
console.log(`响应数据: ${data}`);
});
https 方式
FuRequire.https(files, options, (code, data) => {
console.log(`响应码: ${code}`);
console.log(`响应数据: ${data}`);
});
ts 调用
http方式
import { FuRequire as fu } from "myjs-common";
fu.http(files, options, (code, data) => {
console.log(`响应码: ${code}`);
console.log(`响应数据: ${data}`);
});
https方式
fu.https(files, options, (code, data) => {
console.log(`响应码: ${code}`);
console.log(`响应数据: ${data}`);
});
调用结果输出
响应码: 00000
响应数据: {"code":10000,"message":"success","data":["0.jpg","1.jpg","2.jpg"],"logId":"20191024102400520"}
附录-Java后台代码
@RestController
@RequestMapping("admin/manage")
public class AdminController {
@RequestMapping(value = "upload", method = RequestMethod.POST)
public AIResponse upload(@RequestParam("file")MultipartFile[] files) throws Exception {
List<String> fileNames = Arrays.stream(files).map(MultipartFile::getOriginalFilename).collect(Collectors.toList());
return AIResponse.success("20191024102400520", fileNames);
}
}