Pregunta

I have a link to an excel add-in I let users download. I am using flask + mod_wsgi.

@app.route('/link/')
def download_addin():
    parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/static/'

    response = make_response()
    response.headers['Cache-Control'] = 'no-cache'
    response.headers['Content-Type'] = 'application/vnd.ms-excel.addin.macroEnabled.12'
    response.headers['Content-Disposition'] = 'attachment; filename=my_addin.xlam'
    return response

I download the file but when I use it in Excel I get a warning "Excel cannot open the file 'my_addin.xlam' because the file format or file extension is not valid...'

Thanks!

¿Fue útil?

Solución

You need to return the file as part of the response. Try something like

@app.route('/link/')
def download_addin():
    parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/static/'

    with open(os.path.join(parent_dir, 'my_addin.xlam')) as f:
        response = make_response(f.read())
    response.headers['Cache-Control'] = 'no-cache'
    response.headers['Content-Type'] = 'application/vnd.ms-excel.addin.macroEnabled.12'
    response.headers['Content-Disposition'] = 'attachment; filename=my_addin.xlam'
    return response

Without doing this you'll download an empty file. That's probably why Excel doesn't like the format.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top