문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top