multipart / form-data的示例

时间:2010-11-21 16:09:08

标签: html http multipart multipartform-data

我想知道是否有人可以与我分享包含以下内容的多部分/表单数据示例:

  1. 某些表单参数
  2. 多个文件

3 个答案:

答案 0 :(得分:99)

编辑:我在https://stackoverflow.com/a/28380690/895245

保持类似但更深入的答案

要准确了解发生的情况,请使用nc -l或ECHO服务器以及浏览器或cURL等用户代理。

将表单保存到.html文件:

<form action="http://localhost:8000" method="post" enctype="multipart/form-data">
  <p><input type="text" name="text" value="text default">
  <p><input type="file" name="file1">
  <p><input type="file" name="file2">
  <p><button type="submit">Submit</button>
</form>

创建要上传的文件:

echo 'Content of a.txt.' > a.txt
echo '<!DOCTYPE html><title>Content of a.html.</title>' > a.html

执行命令

nc -l localhost 8000

在浏览器上打开HTML,选择文件并单击“提交”并检查终端。

nc打印收到的请求。 Firefox发送:

POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
Content-Length: 554

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--

另外,cURL应该发送与浏览器表单相同的POST请求:

nc -l localhost 8000
curl -F "text=default" -F "file1=@a.html" -F "file1=@a.txt" localhost:8000

您可以使用以下方式进行多项测试:

while true; do printf '' | nc -l localhost 8000; done

答案 1 :(得分:15)

非常感谢@Ciro Santilli的回答!我发现他对边界的选择非常“不开心”因为所有的连字符:事实上,正如@Fake Name所评论的那样,当你在请求中使用你的边界时,它前面还有两个连字符:

实施例:

POST / HTTP/1.1
HOST: host.example.com
Cookie: some_cookies...
Connection: Keep-Alive
Content-Type: multipart/form-data; boundary=12345

--12345
Content-Disposition: form-data; name="sometext"

some text that you wrote in your html form ...
--12345
Content-Disposition: form-data; name="name_of_post_request" filename="filename.xyz"

content of filename.xyz that you upload in your form with input[type=file]
--12345
Content-Disposition: form-data; name="image" filename="picture_of_sunset.jpg"

content of picture_of_sunset.jpg ...
--12345--

我发现on this w3.org page可以在multipart / form-data中封装multipart / mixed标头,只需在multipart / mixed中选择另一个边界字符串并使用该字符串封装数据。最后,您必须“关闭”FILO命令中使用的所有边界以关闭POST请求(例如:

POST / HTTP/1.1
...
Content-Type: multipart/form-data; boundary=12345

--12345
Content-Disposition: form-data; name="sometext"

some text sent via post...
--12345
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=abcde

--abcde
Content-Disposition: file; file="picture.jpg"

content of jpg...
--abcde
Content-Disposition: file; file="test.py"

content of test.py file ....
--abcde--
--12345--

看看上面的链接。

答案 2 :(得分:0)

有一个多部分数据的例子(Angular):

  1. trip-upload.component.html

    旅行:
       <div class="form-group">
         <label for="guide">Guide for the trip:</label>
         <input formControlName="guide" type="file" id="guide" name="guide" (change)="uploadFile($event,'guide')">
       </div>
    
       <div class="form-group">
         <label for="photo">Guide for the trip:</label>
         <input formControlName="photo" type="image" id="photo" name="photo" (change)="uploadFile($event, 'photo')">
       </div>
    
       <div class="form-group">
         <button class="btn">Upload files</button>
       </div>
     </form>
    

2.trip-upload.component.ts

import {Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from "@angular/forms";
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'trip-upload',
  templateUrl: './trip-upload.component.html',
  styleUrls: ['./trip-upload.component.css']
})

export class TripUploadComponent implements OnInit {
  public form: FormGroup;

  constructor(public fb: FormBuilder, private http: HttpClient) {}

  ngOnInit() {
    this.form = this.fb.group({
      name: [''],
      photo: [null],
      guide: [null]
    })
  }

  uploadFile(event, fileType: string) {
    this.updateFileFormControl(event, fileType);
  }

  submitForm() {
    let formData: any = newFormData();
Object.keys(this.form.controls).forEach(formControlName => {
      formData.append(formControlName, this.form.get(formControlName).value);
    });

    this.http.post('http://localhost:4200/api/trip', formData).subscribe(
            (response) =>console.log(response),
            (error) =>console.log(error)
        )
  }

  private updateFileFormControl(event: Event, formControlName: string) {
    const file = (event.target as HTMLInputElement).files[0];
    this.form.controls[formControlName].patchValue([file]);
    this.form.get(formControlName).updateValueAndValidity()
  }
}
  1. 多部分响应

当浏览器了解您在表单中用于 HTTP POST 请求的编码类型时,用户代理将名称/值对列表配置到服务器。根据传输的数据类型和数量,其中一种方法会比另一种更有效: enter image description here

enter image description here