Answer the question
In order to leave comments, you need to log in
How to return a Word document to the user using jQuery?
I send data to the server - $.post('result_report/', $("#form_extract").serialize(),
The response is returned like this:
response = HttpResponse(content_type='text/docx')
response['Content-Disposition'] = 'attachment; filename="' + form.cleaned_data['cadnum'] + '".docx"'
response.ContentType = "application/ms-word"
return response
And then using jquery I try to let the user save the word: var blob = new Blob([data], {type: 'application/docx'})
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "filename.docx";
link.click();
Ward saved!!!
But when you open it, it says - "Unfortunately, the file cannot be opened due to problems with the content"
Tell me what could be the problem?
Answer the question
In order to leave comments, you need to log in
var blob = new Blob([data], {type: 'application/docx'})
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "filename.docx";
link.click();
This is not a cross browser way, it's better to use
https://github.com/eligrey/FileSaver.js/
"Sorry, the file cannot be opened due to content problems"
The server precisely gives the valid document?
Why all these difficulties with AJAX sending?
Why not just open the form response in a new window the old fashioned way?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question