httputil.createpost如何设置文件最大限制
时间: 2023-09-11 13:10:52 浏览: 41
在使用 `httpUtil.createPost()` 方法时,可以通过设置 `maxBodyLength` 和 `maxContentLength` 选项来限制文件最大大小。其中,`maxBodyLength` 用于限制请求体的最大大小,`maxContentLength` 用于限制整个请求的最大大小。
以下是示例代码:
```javascript
const axios = require('axios');
const http = require('http');
const https = require('https');
const fs = require('fs');
const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
const axiosInstance = axios.create({
httpAgent,
httpsAgent,
maxBodyLength: 50 * 1024 * 1024, // 50MB
maxContentLength: 200 * 1024 * 1024, // 200MB
});
const formData = new FormData();
formData.append('file', fs.createReadStream('/path/to/file'));
const config = {
headers: {
'Content-Type': 'multipart/form-data',
},
};
axiosInstance.post('/upload', formData, config)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error.response.data);
});
```
在上述代码中,`maxBodyLength` 和 `maxContentLength` 分别设置为 50MB 和 200MB,可以根据实际情况进行调整。