Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when upload a big file use multipart form, the memory grow fast and OOM(用multipart 方式上传文件,内存暴涨) #758

Closed
binghan-iOS opened this issue Apr 29, 2020 · 5 comments

Comments

@binghan-iOS
Copy link

binghan-iOS commented Apr 29, 2020

dio version:3.0.0
test platform:simulator iPhone11 pro max(13.4)
flutter version:1.17

client code(dart ):
var dir=(await getApplicationDocumentsDirectory()).path;
final videoName = dir+'/OSX10.15.2.dmg';
final txtName = dir+'/file.txt';
// var imgFile = File(filename);
// Sending stream
var fm = FormData.fromMap({
'name': 'wendux',
'age': 25,
'file': MultipartFile.fromString('hellow world.'),
'files': [
await MultipartFile.fromFile(videoName,
filename: 'OSX10.15.2.dmg'),
MultipartFile.fromFileSync(txtName, filename: 'file.txt'),
]
});

    await dio.post(
"http://localhost:8080/upload",
  data: fm, // Post with Stream<List<int>>
options: Options(
  headers: {
    HttpHeaders.contentTypeHeader: ContentType.binary,
   
   
   // HttpHeaders.contentLengthHeader: imgFile.lengthSync(),
   // HttpHeaders.authorizationHeader: "Bearer $token",
  },
),

).catchError(print);

server code(go):
package main

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)

func uploadHandler(w http.ResponseWriter, r *http.Request) {

reader, err := r.MultipartReader()

if err != nil {
	http.Error(w, err.Error(), http.StatusInternalServerError)
	return
}
for {
	part, err := reader.NextPart()
	if err == io.EOF {
		break
	}

	fmt.Printf("FileName=[%s], FormName=[%s]\n", part.FileName(), part.FormName())
	if part.FileName() == "" { // this is FormData
		data, _ := ioutil.ReadAll(part)
		fmt.Printf("FormData=[%s]\n", string(data))
	} else { // This is FileData
		dst, _ := os.Create("./" + part.FileName() + ".upload")
		defer dst.Close()
		io.Copy(dst, part)
	}
}

}

func main() {
http.HandleFunc("/upload", uploadHandler)
http.ListenAndServe(":8080", nil)
}

@binghan-iOS binghan-iOS changed the title 用multipart 方式上传文件,内存暴涨 when upload a biug file use multipart form, the memory grow fast and OOM(用multipart 方式上传文件,内存暴涨) Apr 29, 2020
@binghan-iOS binghan-iOS changed the title when upload a biug file use multipart form, the memory grow fast and OOM(用multipart 方式上传文件,内存暴涨) when upload a big file use multipart form, the memory grow fast and OOM(用multipart 方式上传文件,内存暴涨) Apr 29, 2020
@100cm
Copy link
Contributor

100cm commented May 14, 2020

你内存能够读几个g....

@binghan-iOS
Copy link
Author

你内存能够读几个g....

我在模拟器上测试的,正常的话上传文件上分块读的。http库上传文件就不会内存爆掉

@stale
Copy link

stale bot commented Jun 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Jun 19, 2020
@stale stale bot closed this as completed Jun 26, 2020
@majingAragon
Copy link

最后怎么解决的?我上传视频文件的时候也遇到out of memory报错了

@cchamm
Copy link

cchamm commented May 18, 2023

這個有解決辦法嗎 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants