Я пытаюсь добиться следующего:
- Создать файл в Django / Python (например, Word Document с модулем python-docx)
- Передать ответ (полный файл) в JavaScript, так что я могу делать другие вещи с этим (например, с API Office Javascript)
Я не уверен, возможно ли это (я видел только способы пройтипростые файлы JSON), но я хотел бы помочь.Теперь я могу создать файл и загрузить его на рабочий стол клиента.Затем клиент должен пройти через свой рабочий стол, чтобы найти файл и загрузить его, чтобы JavaScript мог с ним работать (через FileReader).В идеале я хотел бы напрямую отправить файл в Javascript безо всяких ненужных шагов.Мой код:
В template.html:
# To download the created file from Django/Python
<input type="button" value="Download"
onclick="window.open('filemaker/download_my_file', '_self')">
# To select the file, so Javascript can use it
<input type='file' id='test'>
В template.js:
$(document).ready(function () {
// The document is ready
$('#test').change(insertFile);
});
function insertFile(){
var myFile = document.getElementById("test");
var reader = new FileReader();
reader.onload = function (event) {
// some JavaScript Stuff
};
reader.readAsDataURL(myFile.files[0]);
}
В urls.py:
urlpatterns = [
path('/download_my_file', views.create_file_function),
]
В views.py:
def create_file_function():
document = Document()
document.add_heading('Document Title', 0)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=aass.docx'
document.save(response)
return response
Любые идеи о том, как получить ответ от create_file_function () на var MyFile в insertFile (), будут очень полезны.Большое спасибо за вашу помощь!