【发布时间】:2022-08-06 12:34:02
【问题描述】:
描述你想做什么
在我的一个应用程序中,我需要从我的 Angular 网站将文件上传到我的服务器。 基本上,为了做到这一点,我使用了 FormData 对象,该对象附加了一些信息,如文件名等。 为了发送文件本身,我将在 FormData 中附加一个 fs.readStream()。 然后我通过 axios 将它发布到我的服务器端点。
代码示例(使用表单数据的邮递员请求):
var axios = require(\'axios\');
var FormData = require(\'form-data\');
var fs = require(\'fs\');
var data = new FormData();
data.append(\'avatar\', fs.createReadStream(\'/home/file.mp3\'));
data.append(\'title\', \'test\');
data.append(\'description\', \'test\');
var config = {
method: \'post\',
url: \'localhost:8080/upload-file\',
headers: {
...data.getHeaders()
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
关于服务器,它是在 node.js 中开发的,我使用“multer”中间件来检索文件。
端点代码示例:
import {Response, Request} from \"express\";
public static async UploadFile(req: Request, res: Response): Promise<any> { }
没有 krakend 网关,它可以完美运行,然后我可以在我的端点中检索文件,以便:req.file 发送的其他信息如 \"title\"、\"description\" 在 req.body
使用 krakend,我得到了服务器端除文件之外的所有信息,在请求中,我只找到 req.body 而不是 req.file
所以我的问题是,为什么 krakend 没有将文件数据发送到后端,以及通过 POST 请求 FormData 向 krakend 发送文件的解决方案是什么?
你的配置文件你krakend.json的内容:
{
\"version\": 3,
...
{
\"endpoint\": \"/upload\",
\"method\": \"POST\",
\"output_encoding\": \"no-op\",
\"backend\": [
{
\"method\": \"POST\",
\"encoding\": \"no-op\",
\"url_pattern\": \"/upload-file\",
\"host\": [
\"http://containername:8080\"
]
}
]
}
}
我尝试使用不同的 \"no-op\" 注释,但没有任何效果,我的印象是 krakend 不会解释我的文件上传
使用的命令你是如何启动这个软件的?
I use docker-compose:
krakend:
container_name: \'Gateway\'
image: devopsfaith/krakend
volumes:
- ./KrakenD/dev/:/etc/krakend
ports:
- \"8080:8080\"
- \"1234:1234\"
- \"8090:8090\"
links:
- some containers
- ...
restart: always
network_mode: bridge
日志我没有特定的日志,只有我的后端返回 400 代码,因为它在请求中找不到文件信息。
标签: krakend