{"record":{"id":"a3b94f992e2c92b9","repo":"flipped-aurora/gin-vue-admin","slug":"function-file-open-failed-err-a3b94f","errorCode":null,"errorMessage":"function file.Open() failed, err:","messagePattern":"function file\\.Open\\(\\) failed, err:","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/upload/qiniu.go","lineNumber":42,"sourceCode":"//@function: UploadFile\n//@description: 上传文件\n//@param: file *multipart.FileHeader\n//@return: string, string, error\n\nfunc (*Qiniu) UploadFile(ctx context.Context, file *multipart.FileHeader) (string, string, error) {\n\tputPolicy := storage.PutPolicy{Scope: global.GVA_CONFIG.Qiniu.Bucket}\n\tmac := qbox.NewMac(global.GVA_CONFIG.Qiniu.AccessKey, global.GVA_CONFIG.Qiniu.SecretKey)\n\tupToken := putPolicy.UploadToken(mac)\n\tcfg := qiniuConfig()\n\tformUploader := storage.NewFormUploader(cfg)\n\tret := storage.PutRet{}\n\tputExtra := storage.PutExtra{Params: map[string]string{\"x:name\": \"github logo\"}}\n\n\tf, openError := file.Open()\n\tif openError != nil {\n\t\tlogger.WithCtx(ctx).Mod(\"upload\").Err(openError).Error(\"function file.Open() failed\")\n\n\t\treturn \"\", \"\", errors.New(\"function file.Open() failed, err:\" + openError.Error())\n\t}\n\tdefer f.Close()                                                  // 创建文件 defer 关闭\n\tfileKey := fmt.Sprintf(\"%d%s\", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性\n\tputErr := formUploader.Put(context.Background(), &ret, upToken, fileKey, f, file.Size, &putExtra)\n\tif putErr != nil {\n\t\tlogger.WithCtx(ctx).Mod(\"upload\").Err(putErr).Error(\"function formUploader.Put() failed\")\n\t\treturn \"\", \"\", errors.New(\"function formUploader.Put() failed, err:\" + putErr.Error())\n\t}\n\treturn global.GVA_CONFIG.Qiniu.ImgPath + \"/\" + ret.Key, ret.Key, nil\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@author: [ccfish86](https://github.com/ccfish86)\n//@author: [SliverHorn](https://github.com/SliverHorn)\n//@object: *Qiniu\n//@function: DeleteFile\n//@description: 删除文件\n//@param: key string","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/upload/qiniu.go#L24-L60","documentation":"UploadFile for Qiniu failed when calling file.Open() on the *multipart.FileHeader — the uploaded multipart part could not be opened into a readable file handle before being handed to the qiniu form uploader. This is an application/I-O level failure of the multipart reader, not a Qiniu network error; the message embeds the underlying open error text.","triggerScenarios":"Calling Qiniu.UploadFile when multipart.FileHeader.Open() errors: the request body was already consumed or truncated, multipart parsing limits exceeded, temp-file read failure (disk full, permissions), or client disconnected mid-upload.","commonSituations":"Reverse proxy (nginx) aborting large uploads, server tmp dir full or read-only, mismatched multipart form handling that drained the reader earlier, client timeouts canceling the request body.","solutions":["Check the embedded openError in the log line 'function file.Open() failed' for the root cause (e.g. http: unexpected EOF).","Verify server temp/storage space and permissions for multipart temp files.","Check proxy (nginx `client_max_body_size`) and Gin multipart memory limits vs your file sizes.","Ensure no middleware consumes the request body before the upload handler reads it.","Retry the upload from the client; a disconnected client also yields this failure."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// before calling the uploader, verify the header is readable\nfh, err := header.Open()\nif err != nil {\n    return fmt.Errorf(\"uploaded file unreadable: %w\", err)\n}\nfh.Close()","typeGuard":null,"tryCatchPattern":"path, key, err := q.UploadFile(ctx, fileHeader)\nif err != nil {\n    if strings.Contains(err.Error(), \"file.Open() failed\") {\n        logger.Error(\"multipart read failed (client disconnect or disk issue): %v\", err)\n        c.JSON(400, gin.H{\"code\": 7, \"msg\": \"文件读取失败，请重新上传\"})\n        return\n    }\n    c.JSON(500, gin.H{\"code\": 7, \"msg\": \"上传失败\"})\n}","preventionTips":["Set proxy client_max_body_size and Gin multipart limits above your real max file size.","Monitor server tmp disk usage for multipart temp files.","Do not consume the request body in middleware before handlers run.","Return 4xx (not 5xx) when the embedded error indicates client disconnect/truncation."],"tags":["qiniu","upload","multipart","io"],"backgroundTag":"multipart-file-open-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}