L
L
Larisa .•º2017-01-10 16:05:32
JavaScript
Larisa .•º, 2017-01-10 16:05:32

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

2 answer(s)
A
Alexander Taratin, 2017-01-10
@Taraflex


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?

E
Egor Kazantsev, 2017-01-10
@saintbyte

I recommend playing around with iframe and target

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question