简体   繁体   English

如何将文件从Django后端发送到服务器

[英]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.

相关问题 将压缩的 PDF 文件内容从客户端发送到后端服务器 - Send compressed PDF file contents from client side to backend server 如何将数据从html(Django后端)发送到js(Vue前端) - How to send data from html (Django backend) to js (Vue frontend) 如何将jsPDF转换的pdf文件发送到后端服务器? - How to send a jsPDF converted pdf file to backend server? 无法在 django 后端发送音频文件 - unable to send audio file in django backend HTML 5文件上传以及如何从浏览器向后端发送文件 - Html 5 file upload and how to send a file from a browser to backend 如何从 Express 后端服务器发送和获取数据 - how to send and fetch data from an express backend server 将 MediaRecorder blob 发送到服务器并在后端构建文件 - Send MediaRecorder blobs to server and build file on backend 如何从后端(快递)在nodeJS中创建html文件并将其发送到客户端? - How to create and send html file to client in nodeJS from backend(express)? 如何使用 vue 在 django 后端发送 firebase 令牌? - how to send the firebase token at the django backend with vue? 如何发送 Javascript object 在 Django 后端处理 - How to send an Javascript object to be processed on a Django Backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM