如何在角度2中提交ng2文件上传的帖子数据?

Gha*_*yam 13 ng-file-upload angular

我在角度2中使用ng2-file-upload.有没有办法通过文件上传操作提交表单数据?

Jan*_*yne 26

不是答案,我使用ng2-file-upload遇到了同样的问题.他们有一个叫做的钩子onBeforeUploadItem.我尝试了以下内容.

ngOnInit() {
 this.uploader.onBeforeUploadItem = (fileItem: any) => {
  fileItem.formData.push( { someField: this.someValue } );
  fileItem.formData.push( { someField2: this.someValue2 } );
 };
}
Run Code Online (Sandbox Code Playgroud)

当我注销fileItem.formData所有值的内容都在那里.但是,这些表单元素似乎永远不会回到服务器.我正在使用Chrome,当我观察HTTP帖子时,我看到以下内容.

请求标题

POST /api/upload/csv HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 228
Origin: http://localhost:4200
x-access-token: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyb290IiwiYWRkciI6IjA6MDowOjA6MDowOjA6MSIsInNjaGVtZSI6Imh0dHAiLCJwb3J0IjoiODA4MCIsImlhdCI6MTQ2OTUwMzM1NX0.jICVQdZD-6m705sZsaQJ5-51LztdIx9pAAKgVYgL3HRMMgrJh6ldFbYvUVtA_UQkSrvCrNJeWeo4C7QYe2W4Cw
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryCSUTihSBrgmwjxg1
Accept: */*
Referer: http://localhost:4200/main
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: _ga=GA1.1.941072201.1467616449; token=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJyb290IiwiYWRkciI6IjA6MDowOjA6MDowOjA6MSIsInNjaGVtZSI6Imh0dHAiLCJwb3J0IjoiODA4MCIsImlhdCI6MTQ2OTUwMzM1NX0.jICVQdZD-6m705sZsaQJ5-51LztdIx9pAAKgVYgL3HRMMgrJh6ldFbYvUVtA_UQkSrvCrNJeWeo4C7QYe2W4Cw

请求有效负载

------WebKitFormBoundaryCSUTihSBrgmwjxg1
Content-Disposition: form-data; name="file"; filename="data.csv"
Content-Type: text/csv


------WebKitFormBoundaryCSUTihSBrgmwjxg1--

有什么想法发生了什么?当我上传文件时,我需要使用该文件发送一些额外的表单值.

我非常接近.解决方案是覆盖onBuildItemForm.

ngOnInit() {
 this.uploader.onBuildItemForm = (fileItem: any, form: any) => {
  form.append('someField', this.someValue); //note comma separating key and value
  form.append('someField2', this.someValue2);
 };
}
Run Code Online (Sandbox Code Playgroud)

该实例form的类型为FormData.通过查看我的HTTP帖子,我可以看到我的表单字段值被发送到服务器,我的服务器现在实际上看到了值.希望能帮到你.

  • 只需添加一个可能会引起一些人兴奋的细节:form.append方法是:`form.append('key',value);`*注意名称和值之间的逗号分隔,而不是冒号. (7认同)

Sud*_*van 6

this.uploader.onBeforeUploadItem = (item: FileItem) => {
  item.withCredentials = false;
  this.uploader.authToken = 'Bearer ' + this.boxTokenResponse.userToken;
  this.uploader.options.additionalParameter = {
    name: item.file.name,
    parent_id: this.parentFolderId
  };
};
Run Code Online (Sandbox Code Playgroud)

这就是file-uploader.class.js平移表单数据的方式。

if (!this.options.disableMultipart) {
        sendable = new FormData();
        this._onBuildItemForm(item, sendable);
        sendable.append(item.alias, item._file, item.file.name);
        if (this.options.additionalParameter !== undefined) {
            Object.keys(this.options.additionalParameter).forEach(function (key) {
                sendable.append(key, _this.options.additionalParameter[key]);
            });
        }
    }
Run Code Online (Sandbox Code Playgroud)