vue.js - 在上传图像过程中向服务器发送附加信息
问题描述
我在 vue 2.6.11 上使用 filepond 4.25.1,到目前为止一切正常。我想向我的服务器(即 aspnet core 3)发送附加信息。我从 filepond 发送我的请求,如下所示
myServer: {
url: "http://**********/api/CustomerAuth/",
process: {
url: "uploadimages",
method: "POST",
withCredentials: false,
headers: {},
data: {
nationalcode: "1234567890",
typecode:"1"
},
timeout: 7000,
},
load: (source, load) => {
fetch(source)
.then((res) => res.blob())
.then(load);
},
}
和服务器端
[HttpPost("uploadimages")]
public IActionResult UploadImages()
{
try
{
var file = Request.Form.Files[0];
string folderName = "Upload";
string webRootPath = _hostingEnvironment.WebRootPath;
string newPath = Path.Combine(webRootPath, folderName);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
if (file.Length > 0)
{
string fileName =
ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(newPath, fileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
}
return Ok("Upload Successful");
}
catch (System.Exception ex)
{
return NotFound(new { img_upld_error = ex.Message });
}
}
在服务器端,我需要访问作为数据发送的“国家代码”和“类型代码”,这两个参数的值总是改变,因此它不是静态值,并且这两个参数的用户值的交互总是改变。
如果有人给我一些线索或指导我解决我的问题,我真的很感激。
解决方案
FilePond 开发人员在这里。
data
不作为道具存在process
。
ondata
您可以使用该属性添加其他 FormData 参数。请参阅下面的更新示例:
myServer: {
url: "http://**********/api/CustomerAuth/",
process: {
url: "uploadimages",
method: "POST",
withCredentials: false,
headers: {},
data: {
nationalcode: "1234567890",
typecode:"1"
},
ondata: (formData) => {
formData.append('nationalcode', '1234567890');
formData.append('typecode', '1');
return formData;
}
timeout: 7000,
},
load: (source, load) => {
fetch(source)
.then((res) => res.blob())
.then(load);
},
}
或者,您可以使用 filepond 元数据插件将元数据添加到每个文件(这会自动发送到服务器)。 https://pqina.nl/filepond/docs/patterns/plugins/file-metadata/
FilePond.setOptions({
fileMetadataObject: {
'nationalcode': '1234567890',
'typecode': '1'
}
})
推荐阅读
- rest - 需要一些有关 REST API 的 JWT 身份验证的信息
- matlab - matlab 创建一个结果表以从多个数组中的数据导出到 csv
- r - rstudio - 在数据帧中创建计数器,该计数器根据值或新 ID 的变化进行重置
- firebase - 配置根项目“firebase_auth”时出现问题
- python - 用python中的搭配字典替换文本文件中的所有搭配
- x86 - MASM v6.11 - 准备就绪时敲击键
- python - 如何让我的 python 脚本在我的 GPU 上运行
- java - 扩展类对象java的ID自动递增
- javascript - 叶节点细胞景观的不同行为
- version-control - 有没有办法在 Mercurial 中恢复到早期版本的提交?