如何将密码和其他自定义数据附加到文件对象(Formdata)angular 2

时间:2018-06-08 02:25:08

标签: angular file-upload xmlhttprequest multipartform-data

您好我在angular 2应用程序中上传了多个文件,如下所示

UploadFile(requestParams: any, files: Array<File>) {
        console.log(requestParams, 'rquest came');
        const dataUrl = `/Myproject/uploadDocuments`;
        console.log(dataUrl, 'posting url');
        return Observable.fromPromise(new Promise((resolve, reject) => {
            let formData: any = new FormData(),
                 xhr = new XMLHttpRequest();
            formData.append('applicationId', requestParams.applicationId ? requestParams.applicationId : requestParams);
            formData.append('documentType', 'bestQuote');
            formData.append('user', requestParams.user ? requestParams.user : null );
            for (let i = 0; i < files.length; i++) {
                console.log(i, files[i], 'each file contains');
                formData.append('uploadDocumentList[]', files[i], files[i].name);
            }
            xhr.setRequestHeader('Content-Type', 'multipart/form-data');
            xhr.setRequestHeader('Accept', 'application/pdf');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        console.log(JSON.parse(xhr.response), 'upload response');
                        resolve(JSON.parse(xhr.response));
                    } else {
                        reject(xhr.response);
                    }
                }
            };
            xhr.open('POST', dataUrl, true);
            xhr.send(formData);
        }));
    }

在我的一个组件中,一旦用户输入密码并上传文件,就将密码添加到文件对象中,如下所示

onPasswordChange(event: any, file: any) {
        let password = event.target.value;
        this.fileList.map(obj => {
            if (obj.name === file.name) { obj.password = password; }
        });
    }

但有些如何在后端方面附加密码没有得到。伙计们谁有任何线索在这里出错?在控制台console.log(i, files[i], 'each file contains');中,我可以清楚地看到我的密码已成功通过。但在后端方面它没有得到。有什么想法会出错吗?在此先感谢:)

0 个答案:

没有答案