国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

詳解Django關于StreamingHttpResponse與FileResponse文件下載的最優方法

瀏覽:263日期:2024-09-14 14:07:16
1 StreamingHttpResponse下載

StreamingHttpResponse(streaming_content):流式相應,內容的迭代器形式,以內容流的方式響應。

注:StreamingHttpResponse一般多現實在頁面上,不提供下載。

以下為示例代碼

def streamDownload(resquest): def file_iterator(filepath, chunk_size = 512): with open(filepath, ’rb’) as f: while True: con = f.read(512) if con: yield con else: break filename = os.path.abspath(__file__) + ’test.txt’ response = StreamingHttpResponse(file_iterator(filename) return response # 最后程序會將結果打印在顯示器上2 FileResponse下載

FileResponse(stream):以流形式打開后的文件

注:FileResponse是StreamingHttpResponse的子類

以下為示例代碼:

def homeproc2(request): cwd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) response = FileResponse(open(cwd + '/msgapp/templates/youfile', 'rb')) response[’Content-Type] = ’application/octet-stream’ response[’Content-Disposition’] = ’attachment;filename='filename'’ return response

需要解釋說明的是:

response[’Content-Type] = ’application/octet-stream’ response[’COntent-Disposition’] = ’attachment;filename='filename'’ Content-Type:用于指定文件類型。 COntent-Disposition:用于指定下載文件的默認名稱,對,沒錯! “CO”兩個字符都要大寫。

兩者都是MIME協議里面的標準類型。

到此這篇關于詳解Django關于StreamingHttpResponse與FileResponse文件下載的最優方法的文章就介紹到這了,更多相關Django StreamingHttpResponse與FileResponse內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Django
相關文章:
主站蜘蛛池模板: 沈阳市| 焦作市| 正安县| 双柏县| 苏州市| 辽阳县| 加查县| 罗定市| 灌云县| 若羌县| 榆林市| 察隅县| 长阳| 繁昌县| 潢川县| 鱼台县| 阿巴嘎旗| 黄骅市| 依兰县| 竹北市| 射阳县| 修武县| 来宾市| 威海市| 湖口县| 龙口市| 万荣县| 上饶县| 郸城县| 长武县| 潮安县| 芒康县| 竹山县| 哈尔滨市| 会昌县| 芦溪县| 九江市| 方城县| 鄂尔多斯市| 福海县| 米林县|