Django/jQuery - read file and pass to browser as file download prompt

Posted by danspants on Stack Overflow See other posts from Stack Overflow or by danspants
Published on 2010-06-14T23:18:58Z Indexed on 2010/06/14 23:22 UTC
Read the original article Hit count: 299

Filed under:
|
|
|

I've previously asked a question regarding passing files to the browser so a user receives a download prompt. However these files were really just strings creatd at the end of a function and it was simple to pass them to an iframe's src attribute for the desired effect.

Now I have a more ambitious requirement, I need to pass pre existing files of any format to the browser. I have attempted this using the following code:

def return_file(request):

try:
    bob=open(urllib.unquote(request.POST["file"]),"rb")

    response=HttpResponse(content=bob,mimetype="application/x-unknown")
    response["Content-Disposition"] = "attachment; filename=nothing.xls"
    return HttpResponse(response)
except:
    return HttpResponse(sys.exc_info())

With my original setup the following jQuery was sufficient to give the desired download prompt:

jQuery('#download').attr("src","/return_file/");

However this won't work anymore as I need to pass POST values to the function. my attempt to rectify that is below, but instead of a download prompt I get the file displayed as text.

jQuery.get("/return_file/",{"file":"c:/filename.xls"},function(data)

{

jQuery(thisButton).children("iframe").attr("src",data);

});

Any ideas as to where I'm going wrong?

Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about python