美文网首页
golang 下载文件

golang 下载文件

作者: 阿莱_1b6f | 来源:发表于2018-03-13 16:21 被阅读0次

    //File is found, create and send the correct headers

    //Get the Content-Type of the file
    //Create a buffer to store the header of the file in
    FileHeader := make([]byte, 512)
    //Copy the headers into the FileHeader buffer
    Openfile.Read(FileHeader)
    //Get content type of file
    FileContentType := http.DetectContentType(FileHeader)
    
    //Get the file size
    FileStat, _ := Openfile.Stat()                     //Get info from file
    FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
    
    //Send the headers
    writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
    writer.Header().Set("Content-Type", FileContentType)
    writer.Header().Set("Content-Length", FileSize)
    

    相关文章

      网友评论

          本文标题:golang 下载文件

          本文链接:https://www.haomeiwen.com/subject/fwbzfftx.html