简体   繁体   中英

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. 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM