使用编码为multipart / form-data的数据处理POST请求

时间:2019-02-12 16:37:11

标签: angular typescript go

我正在使用angular和Typescript将我的文件发送到golang的后端的后请求正文中,在那里它解析文件并将文件中的数据上传到DB中。当我从Postman发送文件时,文件已读取且数据已成功插入数据库中,这意味着我的后端代码运行正常。当我从Typescript发送它时,它给了我EOF错误。请在下面找到代码-

角度-

<input (change)="getFile($event.target.value,$event)" type="file" id="fileUpload" name="fileUpload" class="form-control"
            accept=".csv" />

TypeScript-

file: File
getFile(event_value, event){
        this.file = event.target.files[0];
        fileUpload(this.file)
    }

fileUpload(file: any){     
    let url = `abc/abc/abc`
    let header = {'Content-Type':'multipart/form-data; boundary=----abc', 'Authorization': 'authorizationToken'}

    return this.http.post(url, {"file": file}, {headers: header})
    .map((response: Response) => response.json())
    .catch(this.handleError)
  }

Go中的错误:

{"data":{"error":"multipart: NextPart: EOF","code":102,"data":null,"url":""},"level":"error","msg":"{multipart: NextPart: EOF 102 \u003cnil\u003e }","time":"2019-02-12T11:33:00-05:00"}

我是否缺少某些内容,需要在标题中发送?

提前谢谢!

0 个答案:

没有答案
相关问题