I have to upload a file on two places,
in a local directory
in Jira via curl
I have written post endpoint which read files from request and send same file to Jira over a request and after success response, it saves it locally.
my code looks like below
for file in request.files.getlist('file'):
filename = file.filename
mimetype = file.content_type
if not is_valid_type(mimetype):
return json.dumps({"success": False, "message": "Invalid File Format" }), 415
files = {'file': (filename, file, mimetype)}
r = requests.post(jira_url, files=files, headers=headers, auth=HTTPBasicAuth(current_app.config.get('username'), current_app.config.get('password')),verify=False)
LOG.info("Got %s response from %s - text %s", r.status_code, "upload", r.json())
data = r.json()
filename = secure_filename(file.filename)
file.save(os.path.join(current_app.config["UPLOAD_FOLDER"], filename))
it saves the file but when I try to open it, it says we don't support this file format.
if I remove the post call to Jira from loop then it saves the file in the proper format.
did you try with open/write?:
with open("input_file.txt", "w") as text_file:
text_file.write("destination_file")
text_file.close()
solution to specify the path destination:
import os
filename = "input_file.txt"
path = "/pathlocation/..."
fullpath = os.path.join(path, filename)
with open("input_file.txt", "w") as text_file:
text_file.write(fullpath)
text_file.close()
Related
I build an django rest framework that can help me to upload a file
in view.py
class FileResultViewSet(viewsets.ModelViewSet):
queryset = FileResult.objects.order_by('-timestamp')
serializer_class = FileResultSerializer
parser_classes = (MultiPartParser, FormParser)
def perform_create(self, serializer):
serializer.save()
The script work well without any error, and the file is stored in media folder
Then, I use an python script that can help me to upload file to django
import requests
import openpyxl
url = "http://localhost:8000/upload/"
headers = {'Content-Type': 'multipart/form-data'}
file_path = "H:/MEGA_H/Data/uploaded/Book1.xlsx"
'''
with open(file_path, 'rb') as f:
file = {'file': f}
response = requests.get(url, headers=headers, files=file)
'''
filename = "Book1.xlsx"
# Open the file and read its contents
with open(file_path, 'rb') as f:
file_contents = f.read()
# Create a dictionary to store the file information
#file_data = {'file': (filename, file_contents,result_file)}
file_data = {
"result_file": file_contents
}
# Make a POST request to the URL with the file information
response = requests.post(url, files=file_data)
# Check if the request was successful
if response.status_code == 201:
print("File successfully uploaded")
else:
print("Failed to upload file")
The code still works with an issue, after upload the file to media folder, the file doesnt include the file type, so it can not read. I have to change the name of the file , and typing file type ".xlsx"
The file name is only Book1, without Book1.xlsx
with open(file_path, 'rb') as f:
file_contents = f.read()
file_data = {
"result_file": file_contents
}
This is the wrong way to do it. The dict should contain the file object itself, not the file contents.
with open(file_path, 'rb') as f:
file_data = {"result_file": f}
response = requests.post(url, files=file_data)
I am trying to upload a file using Rest to a DJango Python API. But I noticed the file gets modified. Specifically a content-disposition is added to it. I haven't found a good way to remove this. The problem is I am trying to upload a tar that needs to be unzipped, but the modified content prevents unzipping the file.
I’m using this file parser on a rest page:
from rest_framework.parsers import FileUploadParser
The following code seems to get the file for me in the post method of an APIView
file_obj = request.FILES['file']
scanfile.file.save(file_obj.name, file_obj)
Where scanfile is a model with a file field.
The file gets saved with contents like this:
--b3c91a6c13e34fd5a1e253b1a72d63b3
Content-Disposition: form-data; name="file"; filename="sometar.tgz"
My tar file contents here.....
--b3c91a6c13e34fd5a1e253b1a72d63b3
My client looks like this:
filename = "sometar.tgz"
exclusion = "../../exclusionlist.txt"
headers = {'Content-Type': 'multipart/form-data;’,
'Authorization': 'JWT %s' % token,
}
url = "http://localhost:%s/api/scan/Project/%s/" % (port, filename)
#files = {'file': open(filename, 'rb'), 'exclusion_file': open(exclusion, 'rb')} # also tried this way but it just put the info in the same file and I see the headers in the file
files = [('file', open(filename, 'rb')), ('file', open(exclusion, 'rb'))]
x = requests.post(url, files=files, headers=headers)
So my question is how do I remove that content-disposition info from the saved file so I can properly unzip the file?
request.FILES['file'] is an UploadedFile object. You can get its name with request.FILES['file'].name and get just the content with request.FILES['file'].read().
You should be careful with read() and big files:
Read the entire uploaded data from the file. Be careful with this
method: if the uploaded file is huge it can overwhelm your system if
you try to read it into memory. You’ll probably want to use chunks()
instead; see below.
https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.FILES
https://docs.djangoproject.com/en/1.11/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile
I am writing python script to take one by one file from dir and get it mimetype if it's mimetype is not JSON then i want to ignore it. See below part of my script
for filepath in files:
filename = os.path.basename(filepath)
mimetype = mimetypes.guess_type(filepath, strict=False) //here i want to filter out only JSON file and ignore other one
version = "0"
checksum = "0"
fileext = os.path.splitext(filename)[1].lower()
# get raw file data
with open(filepath, "rb") as fr:
filedata = fr.read()
oldfilesize = len(filedata)
See my comment in above code.. Any resolution???
You could try something like this:
for filepath in files:
filename = os.path.basename(filepath)
mimetype = mimetypes.guess_type(filepath, strict=False)
if mimetype != ('application/json', None):
with open(filepath) as f:
try:
json.load(f)
except ValueError:
# It's not json
continue
# do stuff
but this could be inefficient if there are lots of files, and/or they are large.
Well, mimetypes won't help because the mime type application/json for .json files is not inherent to the file metadata. Rather you use it to to provide file type info to whoever is going to to handle it, e.g Content-Type: application/json in the HTTP response header tells the client that it is JSON.
Anyway, the solution might be as follows,
import json
with open("filename", "rt") as f:
try:
d = json.load(f) # no need to name it if you are just checking
except JSONDecodeError:
# handle it or just pass
else:
# Got a json file, do whatever
I'm trying to serve a txt file generated with some content and i am having some issues. I'vecreated the temp files and written the content using NamedTemporaryFile and just set delete to false to debug however the downloaded file does not contain anything.
My guess is the response values are not pointed to the correct file, hense nothing is being downloaded, heres my code:
f = NamedTemporaryFile()
f.write(p.body)
response = HttpResponse(FileWrapper(f), mimetype='application/force-download')
response['Content-Disposition'] = 'attachment; filename=test-%s.txt' % p.uuid
response['X-Sendfile'] = f.name
Have you considered just sending p.body through the response like this:
response = HttpResponse(mimetype='text/plain')
response['Content-Disposition'] = 'attachment; filename="%s.txt"' % p.uuid
response.write(p.body)
XSend requires the path to the file in
response['X-Sendfile']
So, you can do
response['X-Sendfile'] = smart_str(path_to_file)
Here, path_to_file is the full path to the file (not just the name of the file)
Checkout this django-snippet
There can be several problems with your approach:
file content does not have to be flushed, add f.flush() as mentioned in comment above
NamedTemporaryFile is deleted on closing, what might happen just as you exit your function, so the webserver has no chance to pick it up
temporary file name might be out of paths which web server is configured to send using X-Sendfile
Maybe it would be better to use StreamingHttpResponse instead of creating temporary files and X-Sendfile...
import urllib2;
url ="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=s&chld=H|0";
opener = urllib2.urlopen(url);
mimetype = "application/octet-stream"
response = HttpResponse(opener.read(), mimetype=mimetype)
response["Content-Disposition"]= "attachment; filename=aktel.png"
return response
I am trying to create a download of a file object. the file was added using django-filebrowser which means it is turn in to a string path to the the file. I have tried the following:
f = Obj.objects.get(id=obj_id)
myfile = FileObject(os.path.join(MEDIA_ROOT, f.Audio.path))
...
response = HttpResponse(myfile, content_type="audio/mpeg")
response['Content-Disposition'] = 'attachment; filename=myfile.mp3'
return response
The file that is downloaded contains the string of the path to the file location and not the file. Could anyone be of assistance on how to access the file object?
f = Obj.objects.get(id=obj_id)
myfile = open(os.path.join(MEDIA_ROOT, f.Audio.path)).read()
...
response = HttpResponse(myfile, content_type="audio/mpeg")
response['Content-Disposition'] = 'attachment; filename=myfile.mp3'
return response
NOTE! This is not memory friendly! Since the whole file is put into memory. You're better of using a webserver for file serving or if you want to use Django for file serving you could use xsendfile or have a look at this thread
You need to open the file and send it's binary contents back in the response. So something like:
fileObject = FileObject(os.path.join(MEDIA_ROOT, f.Audio.path))
myfile = open(fileObject.path)
response = HttpResponse(myfile.read(), mimetype="audio/mpeg")
response['Content-Disposition'] = 'attachment; filename=myfile.mp3'
return response
Hope that gets what you're looking for.