MarkMelon
2021-01-25 15:43:15 +08:00
partReader, err := ctx.Request.MultipartReader()
if err != nil {
this.logger.WithError(err).Error("UploadChunk invalid http body.")
result.ErrorCode = this.getErrorCodes().ParamError
return
}
for {
part, err := partReader.NextPart()
if part == nil && err == io.EOF {
break
}
if err != nil && err != io.EOF {
this.logger.Error("read body failed, err: ", err)
if err = os.Remove(chunkPath); err != nil {
this.logger.Error("read body failed remove chunk file failed, err: ", err)
}
result.ErrorCode = this.getErrorCodes().ChunkReadError
return
}
if part.FileName() != "" {
// 从 http 流中循环读取并写到文件中
_, err := io.CopyBuffer(file, part, buf)
// _, err := io.CopyBuffer(file, ctx.Request.Body, buf) // 直接解析 http body, 读取 binary 数据,body 中只包含文件内容
if err != nil {
this.logger.Error("write data from part to file failed, err: ", err)
if err = os.Remove(chunkPath); err != nil {
this.logger.Error("write data from part to file failed and remove chunk file failed, err: ", err)
}
result.ErrorCode = this.getErrorCodes().ChunkWriteError
return
}
}
}