我正在尝试建立一个小的应用程序,让你可以上传大块的大文件。我将添加暂停的能力,以及稍后。
我被困在我的循环中的错误:错误处理文件:多部分: NextPart: EOF
func main() {
router := gin.Default()
rg := router.Group("api/v1")
{
rg.POST("/photo", uploadFile)
}
router.Use(CORSMiddleware())
router.Run()
}
func uploadFile(c *gin.Context) {
mr, e := c.Request.MultipartReader()
if e != nil {
panic("Error reading request:" + e.Error())
}
client, e := storage.NewClient(c, option.WithAPIKey(uploadApiKey))
bucket := client.Bucket(uploadBucket)
for {
p, e := mr.NextPart()
if e == io.EOF {
break
} else if e != nil {
panic("Error processing file:" + e.Error())
}
w := bucket.Object(p.FileName()).NewWriter(c)
if _, e := io.Copy(w, p); e != nil {
panic("Error during chunk upload:" + e.Error())
} else if e := w.Close(); e != nil {
panic("Could not finalize chunk writing:" + e.Error())
}
}
}
客户端代码如下所示:
class FileToUpload {
static chunkSize = 512000;
static uploadUrl = 'http://localhost:8080/api/v1/photo';
readonly request: XMLHttpRequest;
readonly file: File;
readonly name: string;
currentChunkStartByte: number;
currentChunkFinalByte: number;
constructor(file: File, name: string) {
this.request = new XMLHttpRequest();
this.file = file;
this.name = name;
this.currentChunkStartByte = 0;
this.currentChunkFinalByte = FileToUpload.chunkSize > this.file.size ? this.file.size : FileToUpload.chunkSize;
}
uploadFile() {
let chunk: Blob = this.file.slice(this.currentChunkStartByte, this.currentChunkFinalByte);
this.request.overrideMimeType('application/octet-stream');
this.request.open('POST', FileToUpload.uploadUrl, true);
const randomNum = Math.random().toString().substr(2);
this.request.setRequestHeader('Content-Type', 'multipart/form-data; boundary=--'+randomNum);
this.request.setRequestHeader('Content-Range', `bytes ${this.currentChunkStartByte}-${this.currentChunkFinalByte}/${this.file.size}`);
this.request.onload = () => {
if(this.currentChunkFinalByte === this.file.size) {
// Do something once done with file
return;
}
this.currentChunkStartByte = this.currentChunkFinalByte;
this.currentChunkFinalByte = this.currentChunkStartByte + FileToUpload.chunkSize;
this.uploadFile();
}
this.request.send(chunk);
}
}
我已经检查过EOF了,我不明白为什么还会有这个错误。有什么想法吗?
发布于 2020-03-10 02:31:07
根据源代码,只有在流体中找不到关闭边界的情况下,才能返回包含io.EOF
的错误,但是客户端标记的主体为发送的。在您的示例中,要么请求正文中缺少标记文件内容结束的边界,要么没有在服务器端解析它。
源代码:https://golang.org/src/mime/multipart/multipart.go#L339
在您的具体情况下,http.Request
的MultipartReader()
会预先解析您的边界,这样您就不需要在那里做任何额外的事情了(https://golang.org/src/net/http/request.go#L486)。同时,MultipartReader()
没有看到任何将文件边界附加到客户端上的流的代码。
不幸的是,这里没有给出负责将内容写入主体的代码,因此我不能在这里提示解决方案,但我非常肯定,客户端随机生成的边界不会传递到除Content-Type
header之外的其他任何地方,这是您面临问题的主要原因。
请阅读更多关于多部分表单如何工作的信息,特别是在以下答案中:https://stackoverflow.com/a/8660740/8008395
https://stackoverflow.com/questions/60586144
复制相似问题