How to download file in browser from Go server

前端 未结 3 1768
走了就别回头了
走了就别回头了 2020-12-12 18:24

My code get file from remote url and download file in browser:

func Index(w http.ResponseWriter, r *http.Request) {
    url := \"http://upload.wikimedia.org/         


        
3条回答
  •  执笔经年
    2020-12-12 18:53

    What you need to use is the Content-Disposition header with attachment value. MDN web docs says the following about the header:

    [...], the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

    If you want to specify a filename for the clients you can use the filename directive. In the following format:

    Content-Disposition: attachment; filename="filename.jpg"
    

    This is an optional parameter and it has some restrictions.

    [...] The filename is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done. [...]

    To format filename properly in the header you should use mime.FormatMediaType. Example:

    cd := mime.FormatMediaType("attachment", map[string]string{"filename": d.Name()})
    w.Header().Set("Content-Disposition", cd)
    

    In this case for content type you can use application/octet-stream because the browser does not have to know the MIME type of the response.

    [...] Generic binary data (or binary data whose true type is unknown) is application/octet-stream. [...]

    w.Header().Set("Content-Type", "application/octet-stream")
    

    For outputing the content of a. file I would recommend to use http.ServeContent or http.ServeFile because they handle RFC 7233 - Range Requests out of the box. Example:

    f, err := fs.Open(name)
    // [...]
    cd := mime.FormatMediaType("attachment", map[string]string{"filename": d.Name()})
    w.Header().Set("Content-Disposition", cd)
    w.Header().Set("Content-Type", "application/octet-stream")
    http.ServeContent(w, r, d.Name(), d.ModTime(), f)
    

提交回复
热议问题