赞
踩
方案一:
- const axios = require('axios')
- const FormData = require('form-data')
- const fs = require('fs')
-
-
-
- const sdUpscaleOnAzure = async (req, res) => {
- const data = new FormData()
- data.append('image', fs.readFileSync('/temp/ai/sd/download/1.png'))
- let config = {
- headers: {
- ...data.getHeaders(),
- Accept: 'application/json',
- },
- method: 'post',
- }
-
- config.url = 'https://www.baidu.com'
- config.data = data
- console.log(config)
-
- axios({
- ...config,
- })
- .then(async (response) => {
- res.send({
- code: 200,
- data: {
- response: response.data,
- },
- message: '成功',
- })
-
- })
- .catch((err) => {
- res.send({
- code: 400,
- data: {
- err,
- },
- message: '失败2',
- })
- })
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
方案二:
- const data = new FormData()
- let tempData = config.data
- for (let key in tempData) {
- data.append(key, tempData[key])
- }
- config.data = data
- config.headers['Content-Type'] =
- 'multipart/form-data; boundary=' + data.getBoundary()
-
- axios({
- ...config,
- })
- .then((response) => {
- res.send({
- code: 200,
- data: {
- response: response.data,
- },
- message: '成功',
- })
- })
- .catch((err) => {
- res.send({
- code: 400,
- data: {
- err,
- },
- message: '失败',
- })
- })
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
参考链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。