0

I have a django app, with a vuejs frontend.

I'm making a PUT request from my Vue front-end. As a response, I want a xlsx file to be generated & downloaded.

For some reason, the file isn't being downloaded. Here's the generate & download function.

def export_to_main(request):
    if request.method == 'PUT':
        response = HttpResponse(content_type='application/vnd.ms-excel')
        response['Content-Disposition'] = 'inline; filename="exported_to_main.xls"'
        selected_claims = Claim.objects.filter(status='PA')
        workbook = Workbook(encoding='utf-8')
        group_header_style = easyxf(
            "font: bold true; pattern: back-colour blue;")
        number_style = easyxf(num_format_str="#,##0.00")
        date_style = easyxf(num_format_str='DD-MM-YYYY')

        sheet = FitSheetWrapper(workbook.add_sheet("Sheet 1"))
        header_column = count()
        sheet.write(0, header_column.next(), 'Book Number', group_header_style)
        sheet.write(0, header_column.next(), 'Dossier', group_header_style)
        sheet.write(0, header_column.next(), 'Dossier Type', group_header_style)

        for i, claim in enumerate(selected_claims, 1):
            try:
                column = count()
                sheet.write(i, column.next(), claim.book_nr)
                sheet.write(i, column.next(), claim.kind)
                sheet.write(i, column.next(), getattr(claim.letter_claim_type, 'label'))
            except IndexError:
                print("error"*5)
                pass

        # selected_claims.update(status='AP', exported_to_main=datetime.datetime.now())
        workbook.save(response)
        return response

If I print(response) I get the workbook in my console. So the excel is generated properly.

For some reason, it is not being downloaded on the frontend though.

Here's the vue code:

<v-row>
        <v-btn 
            color="success"
            @click="exportMain"
            download
            >
            Export to main
        </v-btn>
        <v-spacer></v-spacer>
        <v-btn 
            color="success"
            :href="xls">
            Export Excel
        </v-btn>
        
    </v-row>

and this method is being called on that button click:

export_to_main({commit}) {
                    const url = action="{% url 'control:export-to-main' %}"
                    commit('set_loading', true)
                    axios.defaults.xsrfCookieName = 'csrftoken'
                    axios.defaults.xsrfHeaderName = "X-CSRFTOKEN"
                    axios.put(url, { 'contentType': 'application/json' })
                    .then( (response) => {
                        commit('reset_error')
                    })
                    .catch( (error) => {
                        console.log(error)
                        this.error = error
                    })
                    .finally (() => {
                        commit('set_loading', false)
                    })
                },

Edit: I observed that if I add a button like this, the file is being downloaded. So I think it's a frontend problem.

<a href="/analyzer/central-dev/demoproject/dossiers/export_to_main/">
    test
</a>
Octavian Niculescu
  • 1,177
  • 1
  • 3
  • 24
  • don't you get error message when you save it `workbook.save(response)` ? As for me `response` is HTTP response, not filename, and you should save data in file and later read it to send it - OR maybe you should use `return workbook.save(response)`. – furas Apr 28 '22 at 20:17
  • your code probably reads file from server - but it doesn't mean that it will automatically send to user as download. It simply keep it in memory. Maybe you should rather redirect to url `/analyzer/central-dev/demoproject/dossiers/export_to_main/export_to_main` and then browser will read it and it automatically sends it to user as download. See similar problem with `fetch()` - [javascript - How can I download a file using window.fetch? - Stack Overflow](https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch) - it generates `window.location` with file data – furas Apr 28 '22 at 20:21
  • @furas return workbook.space(response) didnt work (I've got an error). I didn't get any error prior to that. Also, shouldn't the server send the file, by returning the response? – Octavian Niculescu Apr 28 '22 at 20:42
  • There's also [`FileResponse`](https://docs.djangoproject.com/en/4.0/ref/request-response/#fileresponse-objects) that may be more appropriate here. – STerliakov Apr 28 '22 at 23:42

0 Answers0