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

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

golang - 如何檢查 multipart.File 信息

golang - 如何檢查 multipart.File 信息

Go
眼眸繁星 2021-06-19 08:11:12
當(dāng)用戶(hù)上傳文件時(shí),使用 r.FormFile("file") 會(huì)得到一個(gè) multipart.File、一個(gè) multipart.FileHeader 和一個(gè)錯(cuò)誤。我的問(wèn)題是如何只獲取有關(guān)上傳文件的信息,例如,它的大小,如果它是一個(gè)圖像,它的維度等等。我真的不知道從哪里開(kāi)始,所以任何幫助都會(huì)很棒。
查看完整描述

3 回答

?
阿晨1998

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

我發(fā)現(xiàn)對(duì)于此類(lèi)測(cè)試非常簡(jiǎn)單的另一種方法是將測(cè)試資產(chǎn)放置在相對(duì)于包的 test_data 目錄中。在我的測(cè)試文件中,我通常會(huì)創(chuàng)建一個(gè)幫助程序來(lái)創(chuàng)建 *http.Request 的實(shí)例,它允許我在 multipart.File 上非常輕松地運(yùn)行表測(cè)試(為簡(jiǎn)潔起見(jiàn),已刪除錯(cuò)誤檢查)。


func createMockRequest(pathToFile string) *http.Request {

    file, err := os.Open(pathToFile)

    if err != nil {

        return nil

    }

    defer file.Close()


    body := &bytes.Buffer{}

    writer := multipart.NewWriter(body)

    part, err := writer.CreateFormFile("file", filepath.Base(pathToFile))

    if err != nil {

        return nil

    }

    _, _ = io.Copy(part, file)


    err = writer.Close()

    if err != nil {

        return nil

    }

    // the body is the only important data for creating a new request with the form data attached

    req, _ := http.NewRequest("POST", "", body)

    req.Header.Set("Content-Type", writer.FormDataContentType())

    return req

}


查看完整回答
反對(duì) 回復(fù) 2021-06-21
  • 3 回答
  • 0 關(guān)注
  • 453 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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