第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

多部分編寫(xiě)器創(chuàng)建表單文件卡住

多部分編寫(xiě)器創(chuàng)建表單文件卡住

Go
慕勒3428872 2022-09-05 10:24:28
嘗試使用 go 發(fā)布多部分/表單數(shù)據(jù)圖像圖像文件從請(qǐng)求客戶端接收,并且已另存為多部分。文件這是我的代碼func postImage(file multipart.File, url string, filename string) (*http.Response, error) {    r, w := io.Pipe()    defer w.Close()    m := multipart.NewWriter(w)    defer m.Close()    errchan := make(chan error)    defer close(errchan)    go func() {        part, err := m.CreateFormFile("file", filename)        log.Println(err)        if err != nil {            errchan <- err            return        }        if _, err := io.Copy(part, file); err != nil {            errchan <- err            return        }    }()    merr := <-errchan    if merr != nil {        return nil, merr    }    resp, err := http.Post(url, m.FormDataContentType(), r)    if err != nil {        return nil, err    }    defer resp.Body.Close()    return resp, err}當(dāng)我嘗試使用它時(shí),它卡在永遠(yuǎn)不會(huì)返回任何東西part, err := m.CreateFormFile("file", filename)任何解決方案?
查看完整描述

1 回答

?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊

使用管道錯(cuò)誤將錯(cuò)誤傳播回主 goroutine。關(guān)閉管道的寫(xiě)入端,以防止客戶端在讀取時(shí)永久阻塞。關(guān)閉管道的讀取側(cè),以確保 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

}


查看完整回答
反對(duì) 回復(fù) 2022-09-05
  • 1 回答
  • 0 關(guān)注
  • 82 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)