I'm using the golang net/http
package to construct a webserver.And now I have to handle big file upload which means that the server may get request with Expect: 100 Continue
.I will not send response to the client until all data has been received.However every time I finished one request and return, the golang will send a response back by default,How can i implement this?
Use request.ParseMultipartForm,
ParseMultipartForm parses a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files. ParseMultipartForm calls ParseForm if necessary. After one call to ParseMultipartForm, subsequent calls have no effect.
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
}
}
at the frontEnd you should have some javascript like this:
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})
}
</div>