我正在尝试找出如何休息call
来创建新图像。我要发送文件数据以及用户的名称和描述。
在这种情况下,请求如何工作?另外,我可以为此使用json吗?
谢谢
答案 0 :(得分:0)
您只需要在FormData对象中附加有关该文件的文件/数据,就像这样:
const file_data = new FormData();
const imgFile = this.uploadInput.files[0]; /*whatever your file is, from the input tag*/
file_data.append('imgFile', imgFile); /*the image file*/
file_data.append('name', "name of file")
file_data.append('description', "description of file");
在服务器端nodeJS上,您可以像这样从请求中提取信息:
var image = req.files.imgFile;
var name = req.body.name;
var description = req.body.description;
/*save'em to database or something*/
您看到FormData中的文件将从req.files中提取,而不是req.body中!
编辑:您可能想在请求中将“ 内容类型”标头设置为“ 多部分/表单数据”