Django/jQuery - read file and pass to browser as file download prompt
- by danspants
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!