javascript - not able to add one string and file to FormData in JS
问题描述
in my case , i need to add one file and one String value to formData. File Value is appending but string value is not appending
const uploadClickHandler=()=>
{
const formData = new FormData();
for(let i = 0; i< files.length; i++) {
formData.append('file', files[i])
}
formData.append('name','ajith')
console.log(formData);
axios.post('http://localhost:8080/hello/',formData,{
headers: {
"Content-Type":"multipart/form-data"
}
})
In server Side, only file is coming, not string value.
@PostMapping(value="/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ArrayList<LoadDirectoryResponse> getFile(@RequestPart(value="file") MultipartFile[] files)
{
System.out.println(files.length);
ArrayList<LoadDirectoryResponse> dirlist = new ArrayList<LoadDirectoryResponse>();
for(int i=0;i<files.length;i++)
{
LoadDirectoryResponse obj = new LoadDirectoryResponse(files[i].getOriginalFilename());
dirlist.add(obj);
}
return dirlist;
it printing length as 1
解决方案
I think you need to pass that string value with body
const uploadClickHandler=()=>
{
const formData = new FormData();
for(let i = 0; i< files.length; i++) {
formData.append('file', files[i])
}
formData.append('name','ajith')
console.log(formData);
axios.post('http://localhost:8080/hello/',formData,{
headers: {
"Content-Type":"multipart/form-data"
},
body:{
name:formData.name
}
})
推荐阅读
- django - 如何自定义 Django loaddata,以便在加载模型数据之前执行逻辑
- objective-c - 找不到子项目的“ProductModuleName-Swift.h”文件
- html - 在图像上放置文本
- r - 在 tbl_stack 的标题中列出总共 N
- javascript - Heroku Node.js 服务器在运行 npm start 脚本时出错
- web-frontend - 如何从图像中删除背景?
- python - 无法在 Jupyter Notebook 中打开数据框
- .net-core - .NET Core API Serilog-ELK VS logstash/fluentd
- html - 使用 html/javascript 抓取数据的 VBA 代码不起作用
- spotfire - TIBCO Spotfire 表达式:如何将一组嵌套列重命名为新名称