[英]How to send a file from Django backend to server
I am sending an ajax request with a file to my backend that processes it. 我正在将ajax请求和一个文件发送到处理它的后端。 After processing it into another form (image into a compressed processed directory), I want to send that back to the user to down load that.
在将其处理为另一种形式(将图像处理为压缩的处理后的目录)之后,我想将其发送回用户以进行下载。 Can someone provide an example view that will, when it receives the file, process the data the send a request that has the file.
有人可以提供一个示例视图,该视图将在接收文件时处理数据并发送具有该文件的请求。 Also, please show me how to download that file in the javascript front end.
另外,请告诉我如何在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
Also the frond end: 还有叶端:
$.ajax{
//sending the data
onSuccess: //show how to download the data here
}
Thank you in advance. 先感谢您。
For anyone else, I have figured this out by using static files. 对于其他任何人,我已经通过使用静态文件解决了这一问题。 Please use the tag with the download option.
请使用带有下载选项的标签。 For an example:
例如:
<a href="{%get_static_prefix%}{{file_name}}/{{file_name}}.pdf" id="printer" download='{{file_name}}.pdf'></a>
Please note that if you wanna automatically download the file for the user, you make a function. 请注意,如果您想自动为用户下载文件,请执行一项功能。 Inside the function type this:
在函数内部键入以下内容:
document.getElementById("<<name of <a> tag, here: printer>>").click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.