如何创建下载服务?

huangapple go评论92阅读模式
英文:

go) how to i make download service?

问题

我可以帮你翻译代码部分,以下是翻译好的内容:

我创建了一个上传服务器
但是我无法创建下载服务器

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/gin-gonic/gin"
)

func uploadHandler(c *gin.Context) {
	uid := c.Request.FormValue("uid")
	file, header, err := c.Request.FormFile("uploadFile")
	filename := header.Filename
	fmt.Println(filename)
	err = os.Mkdir("./upload/"+uid, 777)
	out, err := os.Create("./upload/" + uid + "/" + filename)
	_, err = io.Copy(out, file)
}

func downloadHandler(c *gin.Context) {
	uid := c.Request.FormValue("uid")
	downloadfilename := c.Request.FormValue("downloadfilename")
	filepath := "./upload/" + uid + "/" + downloadfilename
	c.File(filepath)
}

func main() {
	r := gin.Default()
	r.POST("/goupload", uploadHandler)
	r.POST("/godownload", downloadHandler)
	// 监听并在 0.0.0.0:8080 上提供服务
	r.Run()
}

所以,你可以使用 POST 请求发送 uid 和 downloadfilename 来创建下载服务器。

英文:

i make upload server
but i can't make download server

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/gin-gonic/gin"
)

func uploadHandler(c *gin.Context) {
	uid := c.Request.FormValue("uid")
	file, header, err := c.Request.FormFile("uploadFile")
	filename := header.Filename
	fmt.Println(filename)
	err = os.Mkdir("./upload/"+uid, 777)
	out, err := os.Create("./upload/" + uid + "/" + filename)
	_, err = io.Copy(out, file)
}

func main() {
	r := gin.Default()
	r.POST("/goupload", uploadHandler)
	r.POST("/godownload", downloadHandler)
	// listen and server on 0.0.0.0:8080
	r.Run()
}

so, how to i make download server?

i will use post uid & downloadfilename.

答案1

得分: 3

你可以编写一个新的处理程序,并使用http包中的ServeContentServeFile方法。

f, err := os.Open(yourFile)
if err != nil {
    return err
}
defer f.Close()

fileInfo, err := f.Stat()
if err != nil {
    return err
}

http.ServeContent(w, r, fileInfo.Name(), fileInfo.ModTime(), f)

如果要强制浏览器打开下载对话框,可以添加Content-DispositionContent-Type头,并将文件内容写入响应体:

f, err := os.Open(yourFile)
if err != nil {
    return err
}
defer f.Close()

w.Header().Set("Content-Disposition", "attachment; filename=YourFile")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))

io.Copy(w, f)
英文:

You can write an new handler and use ServeContent or ServeFile from http package.

f, err := os.Open(yourFile)
if err != nil {
	return err
}
defer f.Close()

fileInfo, err := f.Stat()
if err != nil {
	return err
}

http.ServeContent(w, r, fileInfo.Name(), fileInfo.ModTime(), f)

To force the browser open the download dialog, add a Content-Disposition and Content-Type headers and write to file content to the body:

f, err := os.Open(yourFile)
if err != nil {
	return err
}
defer f.Close()

w.Header().Set("Content-Disposition", "attachment; filename=YourFile")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))

io.Copy(w, f)

huangapple
  • 本文由 发表于 2016年2月19日 10:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/35496233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定