Http 多部分编写器CreateFormFile卡滞
尝试使用go发布多部分/表单数据图像 从请求客户端接收的图像文件已保存为multipart.file 这是我的密码Http 多部分编写器CreateFormFile卡滞,http,go,multipartform-data,Http,Go,Multipartform Data,尝试使用go发布多部分/表单数据图像 从请求客户端接收的图像文件已保存为multipart.file 这是我的密码 func postImage(文件multipart.file,url字符串,文件名字符串)(*http.Response,error){ r、 w:=io.Pipe() 延迟w.关闭() m:=多部分NewWriter(w) 延迟m.关闭() errchan:=make(chan错误) 延迟关闭(errchan) go func(){ 零件,错误:=m.CreateFormFil
func postImage(文件multipart.file,url字符串,文件名字符串)(*http.Response,error){
r、 w:=io.Pipe()
延迟w.关闭()
m:=多部分NewWriter(w)
延迟m.关闭()
errchan:=make(chan错误)
延迟关闭(errchan)
go func(){
零件,错误:=m.CreateFormFile(“文件”,文件名)
log.Println(错误)
如果错误!=零{
errchan使用管道错误将错误传播回主goroutine。关闭管道的写入端以防止客户端在读取时永久阻塞。关闭管道的读取端以确保goroutine退出
func postImage(file multipart.File, url string, filename string) (*http.Response, error) {
r, w := io.Pipe()
// Close the read side of the pipe to ensure that
// the goroutine exits in the case where http.Post
// does not read all of the request body.
defer r.Close()
m := multipart.NewWriter(w)
go func() {
part, err := m.CreateFormFile("file", filename)
if err != nil {
// The error is returned from read on the pipe.
w.CloseWithError(err)
return
}
if _, err := io.Copy(part, file); err != nil {
// The error is returned from read on the pipe.
w.CloseWithError(err)
return
}
// The http.Post function reads the pipe until
// an error or EOF. Close to return an EOF to
// http.Post.
w.Close()
}()
resp, err := http.Post(url, m.FormDataContentType(), r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return resp, err
}