golang http处理大文件上传

问题描述 投票:0回答:2

我正在使用 golang

net/http
包来构建一个网络服务器。现在我必须处理大文件上传,这意味着服务器可能会收到
Expect: 100 Continue
的请求。在所有数据都完成之前,我不会向客户端发送响应但是每次我完成一个请求并返回时,golang都会默认发送一个响应,我该如何实现呢?

html file-upload webserver go
2个回答
1
投票

我能写的最好的:

func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Expect") == "100-continue" {
    // Manually send the 100 Continue response
    w.WriteHeader(http.StatusContinue)
}

// Process the file upload
file, _, err := r.FormFile("uploadfile")
if err != nil {
    http.Error(w, "Failed to get the uploaded file", http.StatusInternalServerError)
    return
}
defer file.Close()

// Process the file (e.g., save it to disk)
// For demonstration, we just read the file and discard the data
_, err = io.Copy(io.Discard, file)
if err != nil {
    http.Error(w, "Failed to read the uploaded file", http.StatusInternalServerError)
    return
}

// After processing, send a final response
w.WriteHeader(http.StatusOK)
w.Write([]byte("File uploaded successfully"))

}


0
投票

使用request.ParseMultipartForm,

ParseMultipartForm 将请求正文解析为 multipart/form-data。整个请求主体被解析,并且其文件部分的总共 maxMemory 字节存储在内存中,其余部分存储在磁盘上的临时文件中。如果需要,ParseMultipartForm 会调用 ParseForm。一次调用 ParseMultipartForm 后,后续调用无效。

So yo just can do:

    import(
       "ioutil"
       "net/http"
    )
//check all posible errors, I´m assuming you just have one file per key
    func handler(w http.ResponseWriter, r *http.Request) {
        r.ParseMultipartForm(1000000) //1 MB in memory, the rest in disk
        datas := r.MultipartForm
        for k, headers := range datas.File {
            auxiliar, _ := headers[0].Open() //first check len(headers) is correct
            fileName:=headers[0].Filename
            file, _ := ioutil.ReadAll(auxiliar)
            // do what you need to do with the file
    }
}

在前端你应该有一些像这样的javascript:

function handleFile(url,file){
  let data=new FormData();
  data.append("key",file); //this is the key when ranging over map at backEnd
  fetch(url,{method:"PUT",body:data})
}

© www.soinside.com 2019 - 2024. All rights reserved.