如何在Angular中使用httpClient来感应文件?

问题描述 投票:0回答:1

我尝试使用HttpClient发送文件:

 public async uploadProfile(data: UploadProfile): Promise<any> {
    return await this.http.post(this.uploadProfileUrl, data).toPromise();
 }

在发送文件之前,我将其准备为二进制数组:

interface UploadProfile {
    file: any;
}


file = [123, 10, 32, 32, 34, 100, 105, 115, 97, 98, 108, 101, 83, 105, 122, 101, 76, 105, 109]

但是我得到错误:

TypeError: Cannot read property 'toLowerCase' of undefined
    at HttpXsrfInterceptor.intercept (http.js:2177)
angular angular5 angular8 angular-httpclient
1个回答
0
投票

这可能是由于您在请求期间未定义URL。尝试做以下测试:

更改:

 public async uploadProfile(data: UploadProfile): Promise<any> {
    return await this.http.post(this.uploadProfileUrl, data).toPromise();
 }

收件人:

 public async uploadProfile(data: UploadProfile): Promise<any> {
    return await this.http.post('http://yourUrl/resource', data).toPromise();
 }

如果这可行,并且您再也看不到错误,请尝试找出为什么在拨打电话时未定义uploadProfileUrl

© www.soinside.com 2019 - 2024. All rights reserved.