[英]How sent image and JSON form Angular httpClient
我想將一個請求的圖像和JSON發送到服務器。 這是我的JS代碼,我想與Angular一起發送。
function onSubmit(){
var formData = new FormData();
formData.append("file", document.forms["userForm"].file.files[0]);
formData.append('user', new Blob([JSON.stringify({
"firstName": document.getElementById("firstName").value,
"lastName": document.getElementById("lastName").value})],
{type: "application/json"}));
var boundary=Math.random().toString().substr(2);
fetch('/api/cateogry/saveCategory', {
method: 'post',
body: formData}).then(function(response) {
if (response.status !== 200) {
alert("There was an error!");
} else {
alert("Request successful");
}
}).catch(function(err) {
alert("There was an error!");
});;
}
您可以將圖像作為String發送,使用Base64將圖像轉換為String,然后在Json中發送。 看一下這個: 將圖像轉換為角度2中的base64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.