Fai*_*est 4 file-upload xmlhttprequest go typescript
我正在尝试构建一个小型应用程序,允许您分块上传大文件。我还将添加稍后暂停的功能。
我在循环中遇到了这个错误:Errorprocessing file: multipart: 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())
}
}
}
Run Code Online (Sandbox Code Playgroud)
客户端代码如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了 EOF,我不明白为什么我仍然收到此错误。有任何想法吗?
根据源代码,io.EOF只有在流主体中未找到关闭边界但客户端将主体标记为已发送的情况下,才会返回这样的错误。在您的情况下,请求正文中缺少标记文件内容结尾的边界,或者您没有在服务器端解析它。
源代码: https: //golang.org/src/mime/multipart/multipart.go#L339
在您的具体情况下,http.Request'sMultipartReader()会事先解析您的边界,因此您不需要在那里做任何额外的事情(https://golang.org/src/net/http/request.go#L486)。同时,我没有看到任何将文件边界附加到客户端流的代码。
不幸的是,这里没有提供负责将内容写入正文的代码,因此我无法在这里暗示解决方案,但我很确定客户端上随机生成的边界不会传递到Content-Type标头之外的任何其他地方这是您面临问题的主要原因。
请阅读有关多部分表单如何工作的更多信息,特别是在此答案中:/sf/answers/606251831/
| 归档时间: |
|
| 查看次数: |
13499 次 |
| 最近记录: |