1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2025-07-26 17:10:27 +00:00

client can upload now, but the response body is a whole page, not just

the url
This commit is contained in:
Vincent Batts 2013-10-09 17:02:38 -04:00
parent 5ed865d2e4
commit 96a4b57924
4 changed files with 65 additions and 19 deletions

View file

@ -1,28 +1,63 @@
package client
import (
"bufio"
"io/ioutil"
"mime"
"net/http"
"net/url"
"log"
"os"
"path/filepath"
"mime/multipart"
"bytes"
"path"
"io"
)
func PutFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename)
file, err := os.Open(filename)
func NewfileUploadRequest(uri, file_path string, params map[string]string) (*http.Request, error) {
file, err := os.Open(file_path)
if err != nil {
return
return nil, err
}
resp, err := http.Post(host, mime.TypeByExtension(ext), bufio.NewReader(file))
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("filename", path.Base(file_path))
if err != nil {
return
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
contentType := writer.FormDataContentType()
err = writer.Close()
if err != nil {
return nil, err
}
log.Println(uri)
req, err := http.NewRequest("POST", uri, body)
req.Header.Add("Content-Type", contentType)
return req, err
}
func PutFileFromPath(uri, file_path string, params map[string]string) (path string, err error) {
request, err := NewfileUploadRequest(uri, file_path, params)
if err != nil {
return "", err
}
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
return "", err
}
log.Printf("%#v",resp)
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
return "", err
}
return string(bytes), nil
}