1

I a have flask based web service where I trying to download the results to a file to user's desktop (via https).

I tried :

def write_results_to_file(results):
    with open('output', 'w') as f:
     f.write('\t'.join(results[1:]) + '\n')

this method gets activated when I click export button in the ui.

But I am getting :

<type 'exceptions.IOError'>: [Errno 13] Permission denied: 'output'
      args = (13, 'Permission denied')
      errno = 13
      filename = 'output'
      message = ''
      strerror = 'Permission denied' 

Can some one tell me what I am doing wrong here ?

user3407267
  • 1,524
  • 9
  • 30
  • 57
  • Sounds like a filesystem permissions issue. What operating system are you using? Also, are you writing the file locally or sending it back in the response? – sakurashinken Jul 06 '17 at 17:56
  • I am trying to write locally and then send that back to response. Not sure this is the correct way though – user3407267 Jul 06 '17 at 17:59
  • you want to specify the content type in the header and then send the file back in the body. flask provides the send_file method. no need to write to local fs. https://stackoverflow.com/questions/27337013/how-to-send-zip-files-in-the-python-flask-framework – sakurashinken Jul 06 '17 at 18:07

1 Answers1

2

Can some one tell me what I am doing wrong here ?

The function you posted isn't an actual Flask view function (app.route()), so it isn't entirely clear what your server is doing.

This may be closer to the code you need:

@app.route("/get_results")
def get_results():
    tsv_plaintext = ''

    # I'm assuming 'results' is a 2D array
    for row in results:
        tsv_plaintext += '\t'.join(row)
        tsv_plaintext += '\n'

    return Response(
        tsv_plaintext,
        mimetype="text/tab-separated-values",
        headers={"Content-disposition":
                 "attachment; filename=results.tsv"})

(With assistance from Flask: Download a csv file on clicking a button)

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95