新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)IRIS教程:iris 上傳文件
首先我們需要一個簡單的上傳文件網頁,代碼如下

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網站建設、網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的新吳網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
Upload file
在GO語言中寫入上傳文件代碼
package main
import (
"crypto/md5"
"fmt"
"io"
"path/filepath"
"strconv"
"time"
"github.com/kataras/iris/v12"
)
const maxSize = 5 << 20 // 5MB
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./templates", ".html"))
// Serve the upload_form.html to the client.
app.Get("/upload", func(ctx iris.Context) {
// create a token (optionally).
now := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
// render the form with the token for any use you'd like.
// ctx.ViewData("", token)
// or add second argument to the `View` method.
// Token will be passed as {{.}} in the template.
ctx.View("upload_form.html", token)
})
// Handle the post request from the upload_form.html to the server
app.Post("/upload", iris.LimitRequestBodySize(maxSize+1<<20), func(ctx iris.Context) {
// Get the file from the request.
f, fh, err := ctx.FormFile("uploadfile")
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: " + err.Error() + "")
return
}
defer f.Close()
_, err = ctx.SaveFormFile(fh, filepath.Join("./uploads", fh.Filename))
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.HTML("Error while uploading: " + err.Error() + "")
return
}
})
// start the server at http://localhost:8080 with post limit at 5 MB.
app.Listen(":8080" /* 0.*/, iris.WithPostMaxMemory(maxSize))
}
- ?
app.RegisterView(iris.HTML("./templates", ".html"))?:該語句指定iris解析網頁模板 - ?
_, err = ctx.SaveFormFile(fh, filepath.Join("./uploads", fh.Filename))?:該語句拼接字符串用來當做存儲文件的路徑
分享文章:創(chuàng)新互聯(lián)IRIS教程:iris 上傳文件
文章鏈接:http://m.5511xx.com/article/coeogjd.html


咨詢
建站咨詢
