douyaju4259 2019-08-23 01:54
浏览 159
已采纳

我收到一个致命错误:运行时:使用'go-ipfs-api'下载视频时内存不足

I used go-ipfs-api to download a large file from ipfs, web access download.

I get an error fatal error:

runtime: out of memory.

How can I modify my code?

func main()  {
    http.HandleFunc("/", download)

    http.ListenAndServe(":8080", nil)

}

func download(w http.ResponseWriter, r *http.Request) {
    client := shell.NewShell("http://127.0.0.1:5001")

    fd, err := client.Cat("QmTcj7SfRf4vnLnCqnxMT7kutrzFyevjBeT5RCiN9xGAL4")
    if err != nil{
        fmt.Println(err.Error())
        w.WriteHeader(http.StatusInternalServerError)
        return
    }
    defer fd.Close()

    fileName := "demo.mp4"
    // As per RFC6266 section 4.3
    w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+ fileName)

    data, _ := ioutil.ReadAll(fd)

    http.ServeContent(w, r, fileName, time.Now().Local(), bytes.NewReader(data))

    w.WriteHeader(http.StatusOK)
    return
}
  • 写回答

1条回答 默认 最新

  • dry69034 2019-08-23 02:11
    关注

    Instead of ReadAll, you can use io.Copy, but you have to set the content type yourself:

    w.Header.Set("Content-Type",<the content type>)
    io.Copy(w,fd)
    return
    

    I'm not familiar with the library you're using, you may need to call fd.Close() after copy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起