【问题标题】:Force download of files on App Engine在 App Engine 上强制下载文件
【发布时间】:2011-03-05 19:10:37
【问题描述】:

我将如何强制浏览器下载媒体文件而不是尝试流式传输它们?这些是我的应用程序目录中的静态文件。

【问题讨论】:

    标签: python google-app-engine download content-disposition


    【解决方案1】:

    你需要在你的项目中修改app.yaml,将application/octect-stream设置为mime_type。 检查这个例子:

    - url: /download
      static_dir: static/download
      mime_type : application/octect-stream
    

    正如评论中正确指出的那样,在下载时强制使用某个 mime_type 不是一个好主意。 如果您的用户更愿意流式传输您的内容而不是下载它们,这是他们的决定,您不应强迫他们下载您的文件。
    您可以提供两个不同的链接,一个带有流,一个强制保存对话框。

    【讨论】:

    • 伪造 mimetype 来强制下载通常是个坏主意。
    【解决方案2】:

    如果您使用的是 java,那么您可以使用以下代码。

    //Set content type
    resp.setContentType("text/csv" + .getContentType());
    
    //Set header for attachement
    resp.setHeader("Content-Disposition","attachment;filename=Myfile.csv");
    
    //Retrieve file from database and convert to byte
    resp.getOutputStream().write(b.getFile().getBytes());
    
    //Close stream 
    resp.getOutputStream().close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多