简体   繁体   English

将多部分/表单数据发布到端点

[英]Post multipart/form-data to endpoint

I'm trying to create an upload form for excel files with angular 6. I have implemented a file chooser with which i want to upload (post) excel files to a certain endpoint that expects "MULTIPART_FORM_DATA". 我正在尝试为角度为6的excel文件创建一个上传表单。我实现了一个文件选择器,我想使用该文件选择器将excel文件上传(发布)到某个期望“ MULTIPART_FORM_DATA”的端点。 Now i have read that you should not set the content type in the header for angular versions above 5 but if i do not include the content-type in the header the angular application automatically sets it to "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", which the server does not expect and hence results in a "bad request". 现在,我了解到您不应该在头版本中为高于5的角版本设置内容类型,但是如果我在头中不包含内容类型,则角应用程序会自动将其设置为“ application / vnd.openxmlformats-officedocument.spreadsheetml “ .sheet”,服务器不会期望它,因此会导致“错误请求”。 So how can i implement a valid "post" for multipart/form-data with angular 6? 那么,如何为角度为6的multipart / form-data实现有效的“ post”?

the endpoint looks something like this: 端点看起来像这样:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadExcel(
        @FormDataParam("file") InputStream inputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader){...}

the angular component looks something like this: 角度分量看起来像这样:

handleFileInput(event: any): void {
if (!event.target.files.length) {
  return;
}
this.fileToUpload = event.target.files.item(0);}

private uploadFile(): void {
    this.fileService.uploadFile(this.fileToUpload).subscribe(
      (res: any) => {
        console.log('upload succeeded');
      }
    );}

the html form looks something like this: html表单看起来像这样:

<form (submit)="uploadFile()" enctype="multipart/form-data">
<label for="file">choose excel file to upload: </label>
<input type="file" name="file" id="file" accept=".xls,.xlsx" (change)="handleFileInput($event)">
<input type="submit" name="submit" class="submitButton">

and the service looks like this: 服务看起来像这样:

uploadFile(file: File): Observable<any> {
  const fd: FormData = new FormData();
  fd.append('file', file, file.name);
  return this.http.post(this.fileURL, file);

} }

I found the mistake I've made: I passed the wrong argument to the http.post call. 我发现了自己犯的错误:我将错误的参数传递给http.post调用。 The service should of course look like this: 该服务当然应如下所示:

uploadFile(file: File): Observable<any> {
  const fd: FormData = new FormData();
  fd.append('file', file, file.name);
  return this.http.post(this.fileURL, fd);

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA发布请求错误-多部分/表单数据 - VBA post request error - multipart/form-data 如何使用Power Query的Web发布目录/表单数据 - How to POST a multipart/form-data using Power Query's Web.Contents 如何使用python中的POST请求在multipart/form-data中发送excel文件? - How to send excel file in multipart/form-data with POST Requests in python? Powershell:如何在多部分/表单数据请求中发布 excel (.xlsx) 文件? - Powershell: How can I POST excel (.xlsx) files in a multipart/form-data request? 从 ASP.NET Core HttpContext.Request 的 multipart/form-data 内容读取 excel 文件? - Reading excel file from ASP.NET Core HttpContext.Request of multipart/form-data content? 为什么带有multipart / form-data的HttpPost的ActionResult在“ archivoExcel”参数中接收空值? - Why is my ActionResult with an HttpPost with multipart/form-data receiving a null value in the “archivoExcel” parameter? VBA POST请求发送包含上载文件的多部分表单数据 - VBA POST request sending multipart form data with uploading file 如何使用 WinHTTPRequest 在 Excel 中使用 VBA 发送表单数据 POST 请求 - How to send a form-data POST request with VBA in Excel using WinHTTPRequest 有什么方法可以读取通过 Azure 函数中的表单数据接收的 Excel 文件(版本 2) - Is there any way to Read Excel File that Receive via form-data in Azure Function (Version 2) 将表格数据“ POST”到XLS / CSV - “POST” form data to XLS/CSV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM