我正在尝试使用 fetch API 和 formData - angular material 上传文件
i am trying to upload a file using fetch API and formData - angular material
我正在尝试上传图片并将 formData 发送到后端。
我正在使用 formData.append 附加 uploadedFile 和 fetchAPI 来发送 formData。
我的输入如下所示
<input style="display:none" type="file"
(change)="fileChange($event)"
#fileInput>
fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
console.log('formData', formData);
this.handleUpload(formData);
}
}
handleUpload(formData) {
const url = `/upload?`;
let result;
const req = new Request(proxyurl + url,
{
method: 'POST',
headers: {
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('response ', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}
当我点击服务时,我得到状态代码:422 无法处理的实体,响应如下
{"error":{"message":"?exception.illegalargument?"}}
请求负载如下
------WebKitFormBoundary8E02ll3T0mo433bu
Content-Disposition: form-data; name="uploadFile"; filename="Screen Shot 2019-10-04 at 10.49.34 AM.png"
Content-Type: image/png
------WebKitFormBoundary8E02ll3T0mo433bu--
请帮助我了解如何让它工作。
我通过执行以下操作使其工作:
删除了 headers 并检查服务器是否需要上传文件。
下面的工作代码:
fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadedFile', file, file.name);
this.handleUpload(formData);
}
}
handleUpload(formData) {
const url = `/upload?`;
let result;
const req = new Request(url,
{
method: 'POST',
headers: {},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('response data ', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}
当我不添加任何 headers 时它起作用了。
handleUpload(formData) {
const url = `/upload`;
const proxyurl = 'https://cors-anywhere-proxy.herokuapp.com/';
let result;
const req = new Request(proxyurl + url,
{
method: 'POST',
headers: {},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('result.data', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}
我正在尝试上传图片并将 formData 发送到后端。
我正在使用 formData.append 附加 uploadedFile 和 fetchAPI 来发送 formData。
我的输入如下所示
<input style="display:none" type="file"
(change)="fileChange($event)"
#fileInput>
fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
console.log('formData', formData);
this.handleUpload(formData);
}
}
handleUpload(formData) {
const url = `/upload?`;
let result;
const req = new Request(proxyurl + url,
{
method: 'POST',
headers: {
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('response ', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}
当我点击服务时,我得到状态代码:422 无法处理的实体,响应如下
{"error":{"message":"?exception.illegalargument?"}}
请求负载如下
------WebKitFormBoundary8E02ll3T0mo433bu
Content-Disposition: form-data; name="uploadFile"; filename="Screen Shot 2019-10-04 at 10.49.34 AM.png"
Content-Type: image/png
------WebKitFormBoundary8E02ll3T0mo433bu--
请帮助我了解如何让它工作。
我通过执行以下操作使其工作:
删除了 headers 并检查服务器是否需要上传文件。
下面的工作代码:
fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadedFile', file, file.name);
this.handleUpload(formData);
}
}
handleUpload(formData) {
const url = `/upload?`;
let result;
const req = new Request(url,
{
method: 'POST',
headers: {},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('response data ', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}
当我不添加任何 headers 时它起作用了。
handleUpload(formData) {
const url = `/upload`;
const proxyurl = 'https://cors-anywhere-proxy.herokuapp.com/';
let result;
const req = new Request(proxyurl + url,
{
method: 'POST',
headers: {},
body: formData
});
fetch(req)
.then(response => response.text())
.then(() => {
if (result.data) {
console.log('result.data', result.data);
} else {
console.log('request failed');
}
})
.catch(() => console.log('Can\'t access ' + url + ' response. Blocked by browser?'));
}