问题描述
我正在使用"Axios"来调用WCF方法,该方法将参数文件信息和内容作为参数.读取文件并将其作为base64编码的字符串发送.我的问题是,当文件大小超过某个限制时,AXIOS会引发异常:错误:请求正文大于maxBodyLength限制".我查了一下问题,发现所有解决方案都建议增加AXIOS配置对象中的maxContentLength/maxBodyLength参数,但没有成功.在node.js中的以下已实现的测试用例中查找:
I am using "Axios" to call a WCF method that takes as parameter file information and content. The file is read and sent as a base64 encoded string.My issue is that when the file size exceeds a certain limit, AXIOS throws an exception: "Error: Request body larger than maxBodyLength limit".I looked up the issue and found that all solutions suggest increasing the maxContentLength / maxBodyLength parameters in the AXIOS configuration object, but did not succeed.Find Below an implemented test case in node.js:
var axios = require('axios');
var fs = require('fs');
var path = require('path')
enter code here`var util = require('util')
let readfile = util.promisify(fs.readFile)
async function sendData(url,data) {
let params = data
let resp = await axios({
method: 'post',
url: url,
data: JSON.stringify(params),
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
// maxContentLength: 100000000,
// maxBodyLength: 1000000000
}).catch(err => {
throw err;
})
return resp;
}
async function ReadFile(filepath) {
try{
let res = await readfile(filepath,'base64')
let filename = path.basename(filepath).split('.').slice(0, -1).join('.')
let ext = path.extname(filepath)
return {data:res,fext:ext,fname:filename}
let x = 1
}
catch(err)
{
throw err
}
}
(async () => {
try {
let img = await ReadFile('Files/1.pdf')
let res = await sendData('http://183.183.183.242/EMREngineEA/EMRWS.svc/web/EMR_TestUploadImg',img)
console.log(res)
}
catch (ex) {
console.log(ex)
}
}
)();
就我而言,pdf文件为20 MB,运行时会引发错误.错误:请求正文大于maxBodyLength限制"
In my case, the pdf file is 20 MB, upon running, an error is thrown."Error: Request body larger than maxBodyLength limit"
我尝试设置maxContentLength:100000000,maxBodyLength:1000000000如上所述,但没有成功.
I tried to setting the maxContentLength: 100000000, maxBodyLength: 1000000000as presented above, but did not succeed.
我们非常感谢您的帮助.
Your help is appreciated.
推荐答案
在此简单测试中,maxBodyLength似乎对我有用,我将数据上传到本地Express服务器.如果我尝试上传的内容超过了maxBodyLength,则会收到与您相同的错误消息.因此,我怀疑还有更多事情,例如发生在您的情况下的重定向会触发错误.
The maxBodyLength seems to work for me in this simple test, I upload data to a local Express server. If I try to upload more than the maxBodyLength I get the same error you're getting. So I suspect there's something more, like a redirect happening in your case that's triggering the error.
在此处处记录了axios出现的问题,该问题似乎与该问题有关,建议将maxContentLength设置为Infinity(如其他评论者所建议的那样).
There is an issue logged for axios here that seems to reference the problem, it suggests setting maxContentLength to Infinity (as the other commenter suggests).
例如
maxContentLength: Infinity,
maxBodyLength: Infinity
下面的测试代码:
const axios = require("axios");
function generateRandomData(size) {
const a = Array.from({length: size}, (v, k) => Math.floor(Math.random()*100));
return { data: a, id: 1 };
}
async function uploadData(url, size) {
let params = generateRandomData(size);
let stringData = JSON.stringify(params);
console.log(`uploadData: Uploading ${stringData.length} byte(s)..`);
let resp = await axios({
method: 'post',
url: url,
data: stringData,
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
maxContentLength: 100000000,
maxBodyLength: 1000000000
}).catch(err => {
throw err;
})
console.log("uploadData: response:", resp.data);
return resp;
}
uploadData("http://localhost:8080/upload", 10000000);
对应的服务器代码:
const express = require("express");
const port = 8080;
const app = express();
const bodyParser = require('body-parser')
app.use(bodyParser.json({limit: '50mb'}));
app.post('/upload', (req, res, next) => {
console.log("/upload: Received data: body length: ", req.headers['content-length']);
res.json( { status: 'ok', bytesReceived: req.headers['content-length']});
})
app.listen(port);
console.log(`Serving at http://localhost:${port}`);
这篇关于在Axios中增加maxContentLength和maxBodyLength的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!