Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Golang multipart/form-data File Upload
package main
import (
func main() {
fileDir, _ := os.Getwd()
fileName := "upload-file.txt"
filePath := path.Join(fileDir, fileName)
file, _ := os.Open(filePath)
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", filepath.Base(file.Name()))
io.Copy(part, file)
r, _ := http.NewRequest("POST", "", body)
r.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
Copy link

Altiano commented May 14, 2020


Copy link

hhsm95 commented Jun 2, 2020

This example is the best. Thanks!

Copy link

jingyugao commented Sep 7, 2020

Will this code use sendfile to achieve zero copy?

Copy link

EvgTG commented Nov 3, 2020


Copy link

LIMHARRY commented Mar 4, 2021

Thank you soooooooooo much!!

Copy link

magarem commented May 15, 2021

thanks!! :)

Copy link

kabaluyot commented Jun 19, 2021


Copy link

irvanrahadhian commented Jul 30, 2021

Thank you

Copy link

yedamao commented Aug 6, 2021


Copy link

MingyiLiang commented Nov 2, 2021

Do you have any ideas how to send a zip file using multipart writer?

Copy link

AlfarisiN commented Mar 18, 2022

Thanks!! Much appreciated!

Copy link

foofybuster commented Apr 22, 2022

Thanks! it's working well.

Copy link

AnjanaMadu commented May 6, 2022


Copy link

lsdrfrx commented May 12, 2022

Thank you <3

Copy link

Jontes-Tech commented May 24, 2022


Copy link

devnote-dev commented May 27, 2022

Thanks 👍

Copy link

pablodz commented May 31, 2022

it works

Copy link

zhangpengspin commented May 31, 2022

it works

Copy link

Sujaybiradar25 commented Jun 8, 2022


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment