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

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的新吳網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設(shè)合作伙伴!
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解析網(wǎng)頁模板 - ?
_, err = ctx.SaveFormFile(fh, filepath.Join("./uploads", fh.Filename))?:該語句拼接字符串用來當做存儲文件的路徑
當前題目:創(chuàng)新互聯(lián)IRIS教程:iris 上傳文件
文章出自:http://fisionsoft.com.cn/article/coeogjd.html


咨詢
建站咨詢
