I know about content-disposition but I read what it uses for email messages. And I want to know how I can set file name with content-type.
ps I use Pyramid framework
edit:
Web site has button 'download' how to perform Response object for file name too, like
return Response(body=f.read(), content_type='application/octet-stream')
and what I need to do for showing correct file name in browser.
You need to set the filename parameter of the Content-Disposition header like so:
response.content_disposition = 'attachment; filename="my_filename.txt"'
Use f string in python like below:
response = HttpResponse(file_data, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="{filename}"'
return response
Related
I've written an API to return a CSV to a user based on content they fill into a form in a web application. When running the code below, I can see the response in my console and am certain that the content is properly being returned by my API, but I can't seem to get the file to automatically start downloading.
csv = final_df.to_csv()
response = make_response(csv)
response.headers['Content-Disposition'] = "attachment; filename=export.csv"
response.headers['Content-type'] = "application/force-download"
return response
A working example of mine differs by quoting the filename (which the Developer Docs hint at being required), and using a correct mimetype.
Try
return bytes(csv, encoding='UTF-8'), 200, {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename="export.csv"'}
I'm building a REST API, and it has to send a file in the response. I do not want to include the file content in the response body. Can we attach files to response ?
If I understood you right, you want to send a file with Content-Disposition header set to 'attachment'. Which instructs the browser to download/save the file, instead of displaying its contents inline on the page.
If that's what you want, then you'll have to do something like this:
from flask import make_response
#app.route('/txt')
def attachment():
resp = make_response('my text file')
resp.headers['Content-Type'] = 'text/plain;charset=UTF-8'
resp.headers['Content-Disposition'] = 'attachment;filename=SmartFileName.txt'
return resp
This is really killing me. I've been dealing with this for days.
When a user download a file from my django web app, I want to notify the uploader that his file has been downloaded by sending a mail. The problem is, If I should download a low file size (489kb), it will send a mail once to the uploader. But if I should download a file size of 3mb or above it will send more than one mail to the uploader.
I just want it to send one mail notification to the uploader per download.
views:
#login_required
def document_view(request,emov_id):
fileload = Emov.objects.get(id=emov_id)
filename = fileload.mov_file.name.split('/')[-1]
filesize=fileload.mov_file.size
response = HttpResponse(fileload.mov_file, content_type='')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
response['Content-Length'] = filesize
send_mail('Your file has just been downloaded',loader.get_template('download.txt').render(Context({'fileload':fileload})),'test#example.com',[fileload.email,])
return response
download.txt
'Your file {{ fileload.name}} have been downloaded!'
How can I send mail per download request?
I would suggest a different approach...
When someone download the file, log the event to a table on your database.
Write the Session ID, the file name, the user name.
Make sure that session_id+file_name+user_name are unique key
This way, you can get much more information that can help you later.
Later on (as a crontab batch, or save listener) send the emails.
You can even send a daily/weekly report and so on...
I think you would solve this problem just with following best practises which say "Do not serve files with Django".
Instead, use X-Sendfile HTTP header in your response and configure your webserver to catch it and serve the file. See this if you're using Apache.
Then, create the response as follows:
response = HttpResponse()
response['X-Sendfile'] = unicode(filename).encode('utf-8')
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
response['Content-length'] = filesize # Optional
return response
I am currently returning a response page as a string but I also want to pass it as an excel file. I am having trouble doing both.
This is my views.py file:
response = HttpResponse(htmlString)
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=example1.xls'
book.save(response)
return response
This only gives me the excel file and not the HtmlString which is because I am reassigning response but I dont know how to include both paramaters.
THanks in advance!!
A HTTP response (as in the HTTP protocol, this is not limited to Django) will be treated by the browser either as a file, or displayed in the browser (html, plain text, etc). You cannot return a response with both.
I am using
response.headers['Content-Type'] = gluon.contenttype.contenttype('.xls')
response.headers['Content-disposition'] = 'attachment; filename=projects.xls'
to generate save as dialog box.
Is there a way to get the selected path by the user?
The browser displays the Save As dialog box to the user, then writes your content into that file. It doesn't inform the server what path the content was saved to. I'm afraid you can't get that information.
If your question is about how to send the file contents to the user, you simply write the content to your response object. The browser takes care of actually writing the file to the path selected by the user.
In Django, you would do something like:
def view(request):
# get the file content from somewhere
response = HttpResponse(file_content, mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=projects.xls'
return response
The browser will then prompt the user for a path and save the file "projects.xls" to that path.