簡體   English   中英

通過REST API在服務器上上傳文件

[英]Upload file on the server through REST API

我正在嘗試通過REST API在服務器上上傳文件。 我的api請求FormData模型,但是我收到400 bad request錯誤。

我在devtools中的請求中的有效負載

------ WebKitFormBoundaryvPiRGxMC3Ru3AoFA內容處置:表格數據; name =“文件”; filename =“ test.jpg”內容類型:image / jpeg

------ WebKitFormBoundaryvPiRGxMC3Ru3AoFA--

我的文檔中的有效載荷

文件:(二進制)

請看下面的一些代碼。

upload.component.html

<input type="file" accept="image/*" (change)="onFileChange($event)">

upload.component.ts

uploadForm = this._fb.group({
    profile: ['']
});
formData: FormData = new FormData();
...
onFileChange(event): void {
    if (event.target.files && event.target.files.length) {
        const file = event.target.files[0];
        this.uploadForm.get('profile').setValue(file);
    };
}
...
upload() {
    this.formData.append('file', this.uploadForm.get('profile').value);
    // API REQUEST
}

`

這是將輸入值更改為FormData的好方法嗎?

您能從以下位置更改您的代碼嗎

this.formData.append('file', this.uploadForm.get('profile'.value);

this.formData.append('file', this.uploadForm.get('profile').value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM