上传的文件内容显示如下
上传后的内容显示如下
server.js:
const http = require("http");
const app = http.createServer();
const fs = require("fs");
app.on("request", (req, res) => {
console.log('服务器接收到请求');
if (req.method == "POST") {
console.log('接收post方法')
var data = [];
req.on("data", (chunk) => {
console.log('data chunk')
data.push(chunk)
})
req.on("end", () => {
console.log('data end', data)
var buffer = Buffer.concat(data);
console.log('buffer', buffer)
fs.writeFile("./tmpfile/a.txt", buffer, (err) => {
console.log('成功吗', err)
if (!err) {
res.writeHead(200, {
'content-type': 'application/json; charset=utf-8'
})
res.end("文件接收成功")
} else {
console.log(err)
res.writeHead(403, {
'content-type': 'application/json; charset=utf-8'
})
res.end("文件接收失败")
}
})
})
}
})
app.listen(3001, () => {
console.log("ok");
})
前端代码:
<template>
<a-upload-dragger
name="file"
:multiple="true"
acions="http://127.0.0.1:3001/"
@change="handleChange"
>
<p class="ant-upload-drag-icon">
<a-icon type="inbox" />
</p>
<p class="ant-upload-text">点击或拖曳文件到此区域上传</p>
<p class="ant-upload-hint">支持单个或批量上传</p>
</a-upload-dragger>
</template>
<script>
import { reactive, toRefs, onBeforeMount, onMounted } from 'vue'
import { message } from "ant-design-vue";
export default {
name: '',
setup() {
const handleChange = (info) => {
const status = info.file.status;
if (status !== 'uploading') {
console.log(info.file, info.fileList);
}
if (status === 'done') {
message.success(`${info.file.name} file upload suceess`)
} else if (status === 'error') {
message.error(`${info.file.name} file upload failed.`)
}
}
return {
handleChange
}
}
};
</script>
<style lang='less'>
</style>
改用了express 及 formidable