如何从多部分/表单数据正文请求中删除内容类型?



我正在尝试将文件数组作为多部分/表单数据上传。 我使用 ngx 文件上传 v6.0.1

这是库示例中略有变化的方法。我正在复制它以显示该文件的类型为文件。

public onFileAdded (files: UploadFile[]) {
for (const droppedFile of files) {
if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
this.certificates$.next({
type: ACTIONS.ADD,
value: new Certificate(file, this.getSizeString(file.size), droppedFile.relativePath)
});
});
}
}

这是提交方法:

public submitCertificates(certificates: Certificate[]): Observable<Certificate[]> {
const url = this.completionCertificatesUrl;
const headers = new HttpHeaders({'Content-Type': 'multipart/form-data'});
const formData = new FormData();
certificates.forEach(certificate => {
formData.append('files', certificate.file, certificate.relativePath);
})
return this.http.post<any>(url, formData, {headers}).pipe(
map(data => data)
);

发送的 Post 请求具有以下数据: 请求有效负载

问题是后端(我无法访问和理解的 Spring 框架)抛出 500 并且不接受此有效负载。 有人告诉我,正确的有效载荷应该如下所示:

Content-Type: multipart/form-data; boundary=79ab782d574b45598e6e50d722985144
--79ab782d574b45598e6e50d722985144
Content-Disposition: form-data; name="files"; filename="file-1.json"
content of file-1 file
--79ab782d574b45598e6e50d722985144
Content-Disposition: form-data; name="files"; filename="fil2-2.zip"
content of file-2 file
--79ab782d574b45598e6e50d722985144--

我的问题是如何从有效负载中删除"内容类型"键?

我确实尝试了很多 append()、delete() 的组合,但没有结果。 另外,我担心的是该文件是否真的附加到此请求中。我可以检查一下吗?

我想知道我是否完全缺少文件的内容,因为在我提供的示例中有一行"file-1 的内容",也许这是表示文件内容的更长字符串的快捷方式。 请帮忙!

我的问题是如何从有效负载中删除"内容类型"键?

查看您设置的位置:

删除此行:

const headers = new HttpHeaders({'Content-Type': 'multipart/form-data'});

从此行中删除headers

return this.http.post<any>(url, formData, {headers}).pipe(

最新更新