summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/technoweenie/multipartstreamer/examples/multipart.go
blob: 971078bec91077d158d3e1d261287b897856519d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"mime/multipart"
	"os"
	"path/filepath"
)

func main() {
	defaultPath, _ := os.Getwd()
	defaultFile := filepath.Join(defaultPath, "streamer.go")
	fullpath := flag.String("path", defaultFile, "Path to the include in the multipart data.")
	flag.Parse()

	buffer := bytes.NewBufferString("")
	writer := multipart.NewWriter(buffer)

	fmt.Println("Adding the file to the multipart writer")
	fileWriter, _ := writer.CreateFormFile("file", *fullpath)
	fileData, _ := os.Open(*fullpath)
	io.Copy(fileWriter, fileData)
	writer.Close()

	fmt.Println("Writing the multipart data to a file")
	output, _ := os.Create("multiparttest")
	io.Copy(output, buffer)
}