how to download using django? - python

I have a download link on one of my webpage in django.
When i download a audio file of 75mb it downloads but nothing in the audio, its 0bytes.
this is my code:
from django.core.servers.basehttp import FileWrapper
import mimetypes
rec_file = settings.WEB_PATH + "/media/recording/" + filename
wrapper = FileWrapper( open(rec_file, "r"))
contentType = mimetypes.guess_type(rec_file)[0]
response = HttpResponse(wrapper, mimetype = "application/force-download")
response['Content-Length'] = os.path.getsize(rec_file)
response['Content-Disposition'] = "attachment; filename=" + filename
return response
i use apache server. can anyone tell me the solution ?

Couldn't say what's wrong with your code, but you shouldn't be doing that anyways. You should serve static files directly from Apache or use X-Sendfile.

Related

How can I upload the file to a desired folder in sharepoint using Python?

I am able to upload file to Documents in SharePoint. But I want to upload it to a specific Folder. I am not sure how can I do that. Any help will be welcomed. I am using the code below to upload files.
def upload_file(ctx, listTitle, path):
list_obj = ctx.web.lists.get_by_title(listTitle)
folder = list_obj.root_folder
ctx.load(folder)
ctx.execute_query()
files = folder.files
ctx.load(files)
ctx.execute_query()
with open(path, 'rb') as f:
content = f.read()
file_creation_information = FileCreationInformation()
file_creation_information.overwrite = True
file_creation_information.url = os.path.basename(path)
file_creation_information.content = content
file_new = files.add(file_creation_information)
ctx.load(files)
ctx.execute_query()
upload_file(ctx,'/Documents/reports/',path)
The code above works fine for upload_file(ctx,'Documents',report)
but doesn't work for upload_file(ctx,'Documents/reports',report) folder. It doesn't work.
Error:
office365.runtime.client_request_exception.ClientRequestException: ('-1, System.ArgumentException', "List 'reports' does not exist at site with URL 'https://sharepoint.com/sites/my_page'.", "404 Client Error: Not Found for url: https://sharepoint.com/sites/my_page/_api/Web/lists/GetByTitle('reports')/RootFolder")
The error you're getting is suggesting that you somehow look for a list called reports. But you want to get a folder-object.
It's not clear which package you're using to connect to sharepoint but the docs say that you need to make an api-call like this:
POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)
Authorization: "Bearer " + accessToken
Content-Length: {length of request body as integer}
X-RequestDigest: "{form_digest_value}"
"Contents of file"
Uploading a file to the Document directory is working because it's a folder inside the the default directory Shared Documents. You can only access folders inside root with the .root_foldercalls.
Edit 2: As I don't know which package you're using (probably Office365-REST) you can upload a file using the endpoint above in a normal requests.post-function.
Should look like this:
import requests
url = f'https://{site_url}/_api/web/GetFolderByServerRelativeUrl('{specific Folder URL}')/Files/add(url='a.txt',overwrite=true)'
headers = {Authorization: "Bearer " + accessToken,
Content-Length: content-length
}
payload = your_byte_string
response = requests.post(url = url, headers=headers, data = payload}

download file via url python

I'm trying to download a file via an url. It is normally downloaded, but the problem is that , when I'm trying to open the file, it has 0 bytes.
Does anyone met this problem, or have idea from where can it come?
This is my code
def download():
file_name = "/opt/static/avatar/20/mouse.png"
response = HttpResponse(content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(path)
return response
NginX does not support X-Sendfile header. You must use X-Accel-Redirect instead:
response['X-Accel-Redirect'] = smart_str(path)

How to upload a file to sharepoint site using python script

Is there a way to upload a file on sharepoint site using python script? I tried installing haufe.sharepoint, but it seems like it failed to fetch ntlm while it was installing, and I can't even use the connector module without having ntlm installed.
I've also tried just saving the excel file to the server location (so save it to directory like \server\sharepointsite\files instead of connecting via the URL) using openpyxl, but it looks like the file remains checked out after the file is saved..
I would appreciate any help. Thanks!!
I'll start by saying this example is adapted from the example for Office365-REST-Python-Client. It works with Sharepoint online using the REST API.
https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/upload_file.py
Example URL you might want to upload to [baseurl][site][folder][file].
https://your_company.sharepoint.com/path/to/site/Shared Documents/file.txt
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
baseurl = 'https://your_company.sharepoint.com'
basesite = '/path/to/site' # every share point has a home.
siteurl = baseurl + basesite
localpath = ./file.txt
remotepath = Shared Documents/file.txt # existing folder path under sharepoint site.
ctx_auth = AuthenticationContext(siteurl) # should also be the siteurl
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.
with open(localpath, 'rb') as content_file:
file_content = content_file.read()
dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
haufe.sharepoint only works for sharepoint lists, but you probably need access to document libraries.
You should use Python Requests with the help of Sharepoint's REST API.
If your sharepoint site doesn't support BasicAuth I recommend the requests_ntlm package.
It didn't work for me due to other reasons, but maybe it helps you out a bit.
You could upload files with SharePlum
install SharePlum: pip install SharePlum and try the code below
import requests
from shareplum import Office365
# Set Login Info
username = '<username>'
password = '<password>'
site_name = '<site_name>'
base_path = 'https://<domain_name>.sharepoint.com'
doc_library = 'Shared%20Documents'
nested_folder = 'Shared%20Documents/<folder1>/<folder2>' #if you want to upload in nested folders else nested_folder = doc_library
file_name = "my_file.zip" #when your file in the same directory
# Obtain auth cookie
authcookie = Office365(base_path, username=username, password=password).GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})
session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post(url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
# Upload file
with open(file_name, 'rb') as file_input:
try:
response = session.post(
url=base_path + "/sites/" + site_name + f"/_api/web/GetFolderByServerRelativeUrl('" + nested_folder + "')/Files/add(url='"
+ file_name + "',overwrite=true)",
data=file_input)
print("response: ", response.status_code) #it returns 200
if response.status_code == '200':
print("File uploaded successfully")
except Exception as err:
print("Something went wrong: " + str(err))
print('File Uploaded Successfully')
I think I might be a bit late in answering this question.
The following solution worked for me-
In the Sharepoint webpage, Go to Library Tools>> Library>> Open with Explorer Command( Its the tiny icon in the bottom right beside Connect to Office command.
The address bar gives us the address that we need to upload the file to. Remember to remove "http:" or "https:" from the address This address is your destination to upload the file.
Subsequently you can use shutil package to upload the file.
import shutil as sl
sl.copy(source,destination)
This should help you upload files to Sharepoint
Disclaimer- This works quite well in Python 3.6
The answers above didn't work for me.
I have found a simple and nice way by just mapping a drive to my sharepoint folder and then I used a copy to that drive.
import subprocess
import shutil
subprocess.call(r'net use Y: http://sharepoint/link/to/your/folder', shell=True)
shutil.copy("link_to_local_file","Y:\\")
Instead of copy, You can also delete files or do anything like a normal folder.
I have created a file in SharePoint site in python via rest api calls. Please find my code below.
def CreateHomePage():
server_relative_url = base_url+ '/_api/web/webinfos'
r1 = requests.get(server_relative_url, auth=HttpNtlmAuth(username, password), headers = headers, verify=True)
value = json.loads(r1.text)
for row in value['d']['results']:
if(row['Title'] == myvars['Site Name'].strip(' \t\n\r')):
Id= row['ServerRelativeUrl']
#Add Template and create file simultaneously
title = myvars['Site Name'].strip(' \t\n\r')
post_url = root_url +'GetFolderByServerRelativeUrl(\'/'+Id+'/Pages\')/Files/add(url=\'Home.aspx\',overwrite=true)'
r2 = requests.post(post_url, auth=HttpNtlmAuth(username, password), headers = headers, verify=True)
logger.debug("Creation of home page %d", r2.status_code)
I have created a script to upload attachment into a SharePoint list
let me know if it works
import requests
from shareplum import Office365
# Obtain auth cookie
authcookie = Office365('https://YOUR-NAME.sharepoint.com', username='YOUR-USERNAME',password='YOUR-PASSWORD').GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})
# dirty workaround.... I'm getting the X-RequestDigest from the first failed call
session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post(url="https://YOUR-NAME.sharepoint.com/sites/YOU-SITE/_api/web/GetFolderByServerRelativeUrl('YOUR-FOLDER')/Files/add(url='a.txt',overwrite=true)",data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
# perform the upload
fileName = 'picture.png'
file_name = 'images.png'
with open(file_name, 'rb') as file_input:
response = session.post(
url="https://YOUR-NAME.sharepoint.com/sites/YOUR-SITE/_api/web/lists/getbytitle('ID-ROW-INTO-SHAREPOINT')/items(4)/AttachmentFiles/add(FileName='" + fileName + "')",data=file_input)
print(response.text)

Directly render pdf from urllib with out saving the file and then make it downloadable in django

I always used to save the files I wanted to make downloadable in django. In my project I used that code for example:
def keyDownload(request, benutzername):
benutzernameKey = benutzername +".key"
fsock = open('/var/www/openvpn/examples/easy-rsa/2.0/keys/'+benutzernameKey, 'r')
response = HttpResponse(fsock, mimetype='application/pgp-keys')
response['Content-Disposition'] = "attachment; filename = %s " % (benutzernameKey)
return response
I got a pdf file which I get through urllib:
url = "http://www.urltomypdf.com"
sock = urllib2.urlopen(url)
with open('report.pdf', 'wb') as f:
while True:
content = sock.read()
if not content: break
f.write(content)
At the moment I am saving the pdf in a file called report.pdf. But my aim is to render it directly to my template with a function in django. Is that possible ?
With the introduction of Django 1.5, the StreamingHttpResponse class has been made available to stream a response based on an iterator. Your view and iterator could look like this:
def stream_pdf(url, chunk_size=8192):
sock = urllib2.urlopen(url)
while True:
content = sock.read(chunk_size)
if not content: break
yield content
def external_pdf_view(request, *args, **kwargs):
url = <url> # specify the url here
response = StreamingHttpResponse(stream_pdf(url), content_type="application/pdf")
response['Content-Disposition'] = "filename='%s'" % <filename> #specify the filename here
return response
Pre Django 1.5, it is still possible to stream a response by passing an iterator to HttpResponse, but there are several caveats. First, you need to use the #condition(etag_func=None) decorator on your view function. Secondly, some middleware can prevent a properly streamed response, so you'll need to bypass that middleware. And finally, a chunk of content only gets send when it reaches a length of 1024 bytes, so chunk_size should be over 1024.

Pass files to remote server from django fileupload

I have a Django form with an fileupload. In the view I want to pass this file to another server with an urllib post request.
I tried to put this file in an ordinary post variable like this.
views.py on first server:
def loadfile(request):
server_url = "foo"
class UploadFileForm(forms.Form):
filename = forms.FileField()
context['fileform'] = UploadFileForm()
#after button is pressed
if request.method == 'POST':
upload_file(context, server_url, request.FILES['filename'])
return render_to_response("bar")
def upload_file(context, server_url, image_data):
#create a temp file to store image on sever
temp = tempfile.NamedTemporaryFile()
for chunk in image_data.chunks():
temp.write(chunk)
temp.flush()
#build filename
origfilename = str(image_data)
extention = origfilename[origfilename.rfind("."):]
filename = uuid.uuid4().hex + extention
#encode image so it can be send
with open(temp.name, "rb") as f:
data = f.read()
encoded_string = base64.urlsafe_b64encode(data)
url = "http://" + server_url + "/uploadimage?filename=" + filename
urllib2.urlopen(url, "img_data="+encoded_string)
temp.close()
This works if the downsteam server is also an django testserver, but with nginx/uwsgi I run into "bad gateway" errors. I think this is because the buffer size of uwsgi is to small.
So a solution would be to make an proper multipart post request.
The question is: How to easily create a multipart urllib request given a django fileupload request?
Use the requests library:
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

Categories

Resources