[英]How to send a file from Django backend to server
我正在將ajax請求和一個文件發送到處理它的后端。 在將其處理為另一種形式(將圖像處理為壓縮的處理后的目錄)之后,我想將其發送回用戶以進行下載。 有人可以提供一個示例視圖,該視圖將在接收文件時處理數據並發送具有該文件的請求。 另外,請告訴我如何在javascript前端下載該文件。
class send_text:
def post(self, request):
file = request.POST["file"]
# Processing…………… file_to_send = <path to file>
# show how to send the file here
還有葉端:
$.ajax{
//sending the data
onSuccess: //show how to download the data here
}
先感謝您。
對於其他任何人,我已經通過使用靜態文件解決了這一問題。 請使用帶有下載選項的標簽。 例如:
<a href="{%get_static_prefix%}{{file_name}}/{{file_name}}.pdf" id="printer" download='{{file_name}}.pdf'></a>
請注意,如果您想自動為用戶下載文件,請執行一項功能。 在函數內部鍵入以下內容:
document.getElementById("<<name of <a> tag, here: printer>>").click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.