在本文中,我们将为您详细介绍无法将多部分表单数据从React正确发送到ExpressJs的相关知识,并且为您解答关于无法将多个primarykey约束添加到表sc的疑问,此外,我们还会提供一些关于an
在本文中,我们将为您详细介绍无法将多部分表单数据从 React 正确发送到 Express Js的相关知识,并且为您解答关于无法将多个primary key约束添加到表sc的疑问,此外,我们还会提供一些关于angularjs – 使用Request Payload中的文件名上传多部分表单数据、AngularJS如何上传多部分表单数据和文件?、dio 3.0发送多部分表单数据请求,响应代码为400、Express Js无法从React Js检索提交的表单数据的有用信息。
本文目录一览:- 无法将多部分表单数据从 React 正确发送到 Express Js(无法将多个primary key约束添加到表sc)
- angularjs – 使用Request Payload中的文件名上传多部分表单数据
- AngularJS如何上传多部分表单数据和文件?
- dio 3.0发送多部分表单数据请求,响应代码为400
- Express Js无法从React Js检索提交的表单数据
无法将多部分表单数据从 React 正确发送到 Express Js(无法将多个primary key约束添加到表sc)
问题在于 the append
method of FormData
接受字符串或单个文件作为值,并且“如果没有指定这些值,则值将转换为字符串”。然而,您正在传递一个 array 文件 (selectedFiles
),它被字符串化为 "[object File]"
(如果一次选择两个文件,它将是 [object File],[object File]
- 只是Array.prototype.toString
和 Object.prototype.toString
做什么)。
你需要做
const UploadMyFiles = () => {
const [userName,setUserName] = userState('TestUser')
const handleUserFileUpload = (selectedFiles) => {
const formData = new FormData()
for (const file of selectedFiles)
formData.append('files',file)
formData.append('userName',userName)
sendDocument(formData).catch(…)
}
return (
<div className='myClass'>Select Bill</div>
<Dropzone
accept={'.pdf'}
onDrop={handleUserFileUpload}
>…</Dropzone>
</div>
)
}
浏览器网络标签
angularjs – 使用Request Payload中的文件名上传多部分表单数据
以下是使用Postman进行测试的方法.我只是使用表单数据:
请注意,请求标头将Content-Type作为multipart / form-data.但Request Payload的文件名和Content-Type为image / png.
这是我的代码:
$http({ method: 'POST',url: ApiUrlFull + 'Job/Item?smartTermId=0&name=aaa1&quantity=1&ApiKey=ABC',headers: { 'Content-Type': undefined },transformRequest: function(data) { var fd = new FormData(); fd.append('file',params.imageData); return fd; } })
params只是imageData中具有文件url的对象.
我的代码也发送类似的URL参数(所以我们可以忽略导致问题).但请求有效负载是base64,它看起来不同,因为它缺少文件名字段.
我对后端没有控制权,它是用.NET编写的.
所以我想我的问题是:使用Angular($http或$resource),如何修改请求,以便我发送正确的Request Payload,就像Postman一样?我无法弄清楚如何对此进行逆向工程.
我试过这个https://github.com/danialfarid/ng-file-upload,它实际上在POST之前先做了OPTIONS请求(假设CORS问题).但服务器为OPTIONS提供了405错误.
解决方法
<input type="file" name="file" onchange="uploadFile(this.files)"/>
在你的代码中:
$scope.uploadFile = function(files) { var fd = new FormData(); //Take the first selected file fd.append("file",files[0]); var uploadUrl = ApiUrlFull + 'Job/Item?smartTermId=0&name=aaa1&quantity=1&ApiKey=ABC'; $http.post(uploadUrl,fd,{ withCredentials: true,headers: {'Content-Type': undefined },transformRequest: angular.identity }).success( ...all right!... ).error( ..damn!... ); };
AngularJS如何上传多部分表单数据和文件?
我是angular.js的初学者,但是对基础知识有很好的了解。
我要做的是上传文件和一些表单数据作为多部分表单数据。我读到这不是angular的功能,但是3rd
party库可以做到这一点。我已经通过git克隆了angular-file-upload,但是仍然无法发布简单的表单和文件。
有人可以提供如何执行此操作的示例,html和js吗?
dio 3.0发送多部分表单数据请求,响应代码为400
如何解决dio 3.0发送多部分表单数据请求,响应代码为400?
String filePath = file.path;
var fileName = filePath.substring(filePath.lastIndexOf("/") + 1,filePath.length);
multipartfile multipartfile = await multipartfile.fromFile(filePath,filename: fileName);
FormData formData = FormData();
formData.fields
..add(MapEntry("name",fileName))
..add(MapEntry("key","res/${aliyunCryp.filename}"))
..add(MapEntry("policy",aliyunCryp.policyBase64))
..add(MapEntry("OSSAccessKeyId",aliyunCryp.accessId))
..add(MapEntry("success_action_status",200.toString()))
..add(MapEntry("callback",aliyunCryp.callback))
..add(MapEntry("signature",aliyunCryp.signature))
..add(MapEntry("file","aliyunCryp.signature"));
formData.files.add(MapEntry("file",multipartfile));
dio dio = dio();
dio.interceptors.add(LogInterceptor(requestBody: true,responseBody: true));
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
client.badCertificateCallback = (X509Certificate cert,String host,int port) {
Logger.print(cert);
//暂时不校验证书
return true;
};
};
dio.options.headers = {"content-type": "multipart/form-data; boundary= ${formData.boundary}"};
dio.post("https://tope-test-baie-bj.oss-cn-beijing.aliyuncs.com/",data: formData).then((value) {
Logger.print("--------log---response--------$value----");
}).catchError((onError) {
Logger.print("--------log---response---error-----$onError----");
});
响应错误日志:
I/Flutter (32443): Response Text:
I/Flutter (32443): <?xml version="1.0" encoding="UTF-8"?>
I/Flutter (32443): <Error>
I/Flutter (32443): <Code>MalformedPOSTRequest</Code>
I/Flutter (32443): <Message>The body of your POST request is not well-formed multipart/form-data</Message>
I/Flutter (32443): <RequestId>5F60B2EB1810433430A77275</RequestId>
I/Flutter (32443): <HostId>tope-test-baie-bj.oss-cn-beijing.aliyuncs.com</HostId>
I/Flutter (32443): </Error>
我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。 我找不到formdata问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Express Js无法从React Js检索提交的表单数据
将body
的{{1}}选项更改为data
。有关如何发送POST请求的信息,请参见docs。
axios
:
api.js
const express = require('express');
const router = express.Router();
router.post('/submit-student',function(req,res) {
console.log(req.body);
res.send(req.body);
});
module.exports = router;
:
server.js
客户端:
const express = require('express');
const cors = require('cors');
const api = require('./api');
const app = express();
const port = 9000;
app.use(cors());
app.use(express.json());
app.use('/api',api);
app.listen(port,() => console.log('server start'));
服务器日志:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios({
url: 'http://localhost:9000/api/submit-student',method: 'POST',data: { name: 'John Doe' }
})
.then(() => {
console.log('Data has been saved.');
})
.catch(() => {
console.log('Internal server error');
});
</script>
</body>
</html>
,
解决方案1:
您需要中间件“ body解析器”
将此添加到您的app.js或server.js文件(服务器端)
/var/www/etc.etc/phpPage.php
解决方案2: (反面)
var bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
在axios中,您发送到服务器的正文应该是原始的,并且调用包含标头function submitForm(){
axios({
url: 'http://localhost:9000/api/submit-student',body: JSON.stringify({name: 'John Doe'}),// body must be raw ( as a plain string should do it )
headers: { 'Content-Type': 'Application/json'} // need the type of the content that is being send
})
.then(() => {
console.log('Data has been saved.')
})
.catch(() => {
console.log('Internal server error')
})
}
关于无法将多部分表单数据从 React 正确发送到 Express Js和无法将多个primary key约束添加到表sc的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于angularjs – 使用Request Payload中的文件名上传多部分表单数据、AngularJS如何上传多部分表单数据和文件?、dio 3.0发送多部分表单数据请求,响应代码为400、Express Js无法从React Js检索提交的表单数据的相关信息,请在本站寻找。
本文标签: