本文整理匯總了Golang中http.Request.FormFile方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.FormFile方法的具體用法?Golang Request.FormFile怎麽用?Golang Request.FormFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類http.Request
的用法示例。
在下文中一共展示了Request.FormFile方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: postHandler
func postHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if cv := req.FormValue("cookie"); cv != "" {
trace("postHandler recieved param cookie %s.", cv)
cp := strings.SplitN(cv, "=", 2)
if cp[1] != "-DELETE-" {
exp := time.SecondsToUTC(time.UTC().Seconds() + 7*24*3600).Format(http.TimeFormat) // Now + 7 days
w.Header().Set("Set-Cookie", fmt.Sprintf("%s=%s; Path=/de/index; expires=%s; Domain=my.domain.org; Secure;", cp[0], cp[1], exp))
} else {
trace("post-handler: Deleting cookie %s\n", cp[0])
w.Header().Set("Set-Cookie", fmt.Sprintf("%s=%s; Path=/de/index; MaxAge=-1; Domain=my.domain.org; Secure;", cp[0], "X"))
}
}
t := req.FormValue("q")
if req.Method != "POST" {
fmt.Printf("====== called /post with GET! ======\n")
}
_, header, err := req.FormFile("datei")
if err == nil {
info("Recieved datei: %s. %v", header.Filename, header.Filename == "file äöü 1.txt")
}
if t != "" {
// w.Header().Set("Location", "http://localhost:54123/"+t)
// w.Header().Set("Location", "localhost:54123/"+t)
w.Header().Set("Location", "/"+t)
w.WriteHeader(302)
} else {
text := req.FormValue("text")
w.WriteHeader(200)
body := "<html><body><h1>Post Page</h1><p>t = " + html.EscapeString(text) + "</p></body></html>"
w.Write([]byte(body))
}
}
示例2: upload
func upload(req *http.Request) (*page, os.Error) {
var p page
p.template = "upload.html"
p.html = make(map[string]interface{})
p.html["Body"] = ""
// show upload form if no post
if req.Method != "POST" {
return &p, nil
}
f, fh, err := req.FormFile("upfile")
if err != nil {
return nil, err
} else {
file, err := os.Create(filesDir + "/" + fh.Filename)
if err != nil {
return nil, err
} else {
io.Copy(file, f)
file.Close()
p.html["Body"] = fh.Filename + " uploaded!"
}
f.Close()
}
return &p, nil
}
示例3: uploadUser
func uploadUser(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
// No upload; show the upload form.
uploadUserTemplate.Execute(w, nil)
return
}
f, _, err := r.FormFile("image")
errors.Check(err)
defer f.Close()
// Grab the image data
i, _, err := image.Decode(f)
errors.Check(err)
var key string = keyOf()
// store image
i = picstore.Store(key, i, 320, "userimg")
i = picstore.Store(key, i, 180, "userthumb")
// generate result
w.Header().Set("Content-Type", "application/json")
w.Header().Set("cache-control", "no-cache")
w.Header().Set("Expires", "-1")
fmt.Fprintf(w, "{\"profilePicUrl\":\"uimg?id="+key+"\",\"profileThumbUrl\":\"utmb?id="+key+"\"}")
}
示例4: uploadHandler
/*
* Handle uploading images
*/
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
return
}
f, fHeader, err := r.FormFile("image")
checkError(err)
defer f.Close()
// check file extension
fileName := strings.TrimSpace(fHeader.Filename)
fileType := strings.ToLower(fileName[len(fileName)-3:])
if fileType != "png" && fileType != "jpg" {
panic(os.NewError("Invalid file type."))
}
t, err := os.Create(UploadDir + fileName)
checkError(err)
defer t.Close()
_, e := io.Copy(t, f)
checkError(e)
// JSON response
jsonResponse, _ := json.MarshalForHTML(&UploadResult{Image: fileName, Error: false, Message: "Image uploaded."})
fmt.Fprint(w, string(jsonResponse))
}
示例5: upload
// upload is the HTTP handler for uploading images; it handles "/".
func upload(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
// No upload; show the upload form.
uploadTemplate.Execute(w, nil)
return
}
f, _, err := r.FormFile("image")
check(err)
defer f.Close()
// Grab the image data
var buf bytes.Buffer
io.Copy(&buf, f)
i, _, err := image.Decode(&buf)
check(err)
// Resize if too large, for more efficient moustachioing.
// We aim for less than 1200 pixels in any dimension; if the
// picture is larger than that, we squeeze it down to 600.
const max = 1200
if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
// If it's gigantic, it's more efficient to downsample first
// and then resize; resizing will smooth out the roughness.
if b.Dx() > 2*max || b.Dy() > 2*max {
w, h := max, max
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i = resize.Resample(i, i.Bounds(), w, h)
b = i.Bounds()
}
w, h := max/2, max/2
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i = resize.Resize(i, i.Bounds(), w, h)
}
// Encode as a new JPEG image.
buf.Reset()
err = jpeg.Encode(&buf, i, nil)
check(err)
// Create an App Engine context for the client's request.
c := appengine.NewContext(r)
// Save the image under a unique key, a hash of the image.
key := datastore.NewKey(c, "Image", keyOf(buf.Bytes()), 0, nil)
_, err = datastore.Put(c, key, &Image{buf.Bytes()})
check(err)
// Redirect to /edit using the key.
http.Redirect(w, r, "/edit?id="+key.StringID(), http.StatusFound)
}
示例6: extractImageFromPost
// 99.9% of this was stolen from
// http://goo.gl/lgkTC
func extractImageFromPost(name string, r *http.Request) []byte {
f, _, err := r.FormFile(name)
check(err)
defer f.Close()
// Grab the image data
buf := new(bytes.Buffer)
io.Copy(buf, f)
i, _, err := image.Decode(buf)
check(err)
// We aim for less than 400 pixels in any dimension; if the
// picture is larger than that, we squeeze it down
const max = 800
if b := i.Bounds(); b.Dx() > max || b.Dy() > max {
// If it's gigantic, it's more efficient to downsample first
// and then resize; resizing will smooth out the roughness.
if b.Dx() > 2*max || b.Dy() > 2*max {
w, h := max, max
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i = resize.Resample(i, i.Bounds(), w, h)
b = i.Bounds()
}
w, h := max/2, max/2
//w, h := max, max
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i = resize.Resize(i, i.Bounds(), w, h)
}
// Encode as a new JPEG image.
buf.Reset()
err = jpeg.Encode(buf, i, &jpeg.Options{Quality: 95})
check(err)
// return JPEG
return buf.Bytes()
}
示例7: storeChunk
// storeChunk puts the chunk in the request into the right place on disk
func storeChunk(tempDir string, tempFile string, ngfd NgFlowData, r *http.Request) error {
err := os.MkdirAll(tempDir, DefaultDirPermissions)
if err != nil {
return errors.New("Bad directory")
}
file, _, err := r.FormFile("file")
if err != nil {
return errors.New("Can't access file field")
}
data, err := ioutil.ReadAll(file)
if err != nil {
return errors.New("Can't read file")
}
err = ioutil.WriteFile(tempFile, data, DefaultDirPermissions)
if err != nil {
return errors.New("Can't write file")
}
return nil
}