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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何處理同時具有多個輸入和多個文件的請求

如何處理同時具有多個輸入和多個文件的請求

Go
青春有我 2022-09-12 20:35:18
構(gòu)建一個后端 go 服務(wù)器,該服務(wù)器可以采用具有多個輸入的形式,其中 3 個具有多個文件輸入。我搜索了一下,它指出,如果你想做這樣的東西,你不想使用典型的if err := r.ParseMultipartForm(32 << 20); err != nil {        fmt.Println(err)    }    // get a reference to the fileHeaders    files := r.MultipartForm.File["coverArt"]相反,您應(yīng)該使用mr, err := r.MultipartReader()    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)    }標(biāo)準(zhǔn)表單數(shù)據(jù):名字電子郵件封面圖片照片(多個文件)個人資料照片(多個文件)2 音頻文件 (2 歌曲)2個視頻(個人介紹,無伴奏合唱中的人物錄音)表單<form method="post" enctype="multipart/form-data" action="/upload">        <input type="text" name="name">        <input type="text" name="email">        <input name="coverArt" type="file"  multiple />        <input name="profile" type="file"  multiple />        <input type="file" name="songs"  multiple />        <input type="file" name="videos"  multiple/>        <button type="submit">Upload File</button> </form>轉(zhuǎn)到代碼:func FilePOST(w http.ResponseWriter, r *http.Request) error {    fmt.Println("File Upload Endpoint Hit")    mr, err := r.MultipartReader()    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)    }        for {        part, err := mr.NextPart()        // This is OK, no more parts        if err == io.EOF {            break        }        // Some error        if err != nil {            http.Error(w, err.Error(), http.StatusInternalServerError)                  }轉(zhuǎn)到服務(wù)器錯誤: 去運(yùn)行 main.go [15:58:21] 現(xiàn)在在以下位置提供服務(wù) www.localhost:3000 文件上傳端點 命中信息[0009] POST /上傳已過=“680.422μs” 主機(jī) = 方法 = POST 路徑=/上傳查詢= 2021/07/14 15:58:32 http: 恐慌服務(wù) [::1]:62924: 運(yùn)行時錯誤: 無效的內(nèi)存地址或 nil 指針取消引用
查看完整描述

1 回答

?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊

很難猜測你的代碼在哪里恐慌。原因可能是您的程序在發(fā)生錯誤時繼續(xù)執(zhí)行。例如,如果創(chuàng)建文件失敗,將死機(jī),因為 為零。outfile.Close()outfile


這兩種方法都支持單個字段的多個文件。不同之處在于它們?nèi)绾翁幚韮?nèi)存。流式處理版本從網(wǎng)絡(luò)讀取一小部分?jǐn)?shù)據(jù),并在您調(diào)用 時將其寫入文件。另一個變體在您調(diào)用 時將所有數(shù)據(jù)加載到內(nèi)存中,因此它需要與要傳輸?shù)奈募笮∫粯佣嗟膬?nèi)存。您將在下面找到兩種變體的工作示例。io.CopyParseMultiForm()


流媒體變體:


func storeFile(part *multipart.Part) error {

    name := part.FileName()

    outfile, err := os.Create("uploads/" + name)

    if err != nil {

        return err

    }

    defer outfile.Close()

    _, err = io.Copy(outfile, part)

    if err != nil {

        return err

    }

    return nil

}


func filePOST(w http.ResponseWriter, r *http.Request) error {

    fmt.Println("File Upload Endpoint Hit")

    mr, err := r.MultipartReader()

    if err != nil {

        return err

    }

    for {

        part, err := mr.NextPart()


        // This is OK, no more parts

        switch {

        case errors.Is(err, io.EOF):

            fmt.Println("done")

            return nil

        case err != nil:

            // Some error

            return err

        default:

            switch part.FormName() {

            case "coverArt", "profile", "songs", "videos":

                if err := storeFile(part); err != nil {

                    return err

                }

            }

        }

    }

}


func main() {

    http.HandleFunc("/upload", func(writer http.ResponseWriter, request *http.Request) {

        err := filePOST(writer, request)

        if err != nil {

            http.Error(writer, err.Error(), http.StatusInternalServerError)

            log.Println("Error", err)

        }

    })

    if err := http.ListenAndServe(":8080", nil); err != nil {

        log.Fatal(err)

    }

}

版本帶有 ,它將數(shù)據(jù)讀取到內(nèi)存。ParseMultipartForm


func storeFile(part *multipart.FileHeader) error {

    name := part.Filename

    infile, err := part.Open()

    if err != nil {

        return err

    }

    defer infile.Close()

    outfile, err := os.Create("uploads/" + name)

    if err != nil {

        return err

    }

    defer outfile.Close()

    _, err = io.Copy(outfile, infile)

    if err != nil {

        return err

    }

    return nil

}


func FilePOST(w http.ResponseWriter, r *http.Request) error {

    fmt.Println("File Upload Endpoint Hit")

    if err := r.ParseMultipartForm(2 << 24); err != nil {

        return err

    }

    for _, fileType := range []string{"coverArt", "profile", "songs", "videos"} {

        uploadedFiles, exists := r.MultipartForm.File[fileType]

        if !exists {

            continue

        }

        for _, file := range uploadedFiles {

            if err := storeFile(file); err != nil {

                return err

            }

        }

    }

    return nil

}


func main() {

    http.HandleFunc("/upload", func(writer http.ResponseWriter, request *http.Request) {

        err := FilePOST(writer, request)

        if err != nil {

            http.Error(writer, err.Error(), http.StatusInternalServerError)

            log.Println("Error", err)

        }

    })

    if err := http.ListenAndServe(":8080", nil); err != nil {

        log.Fatal(err)

    }

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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