Problem Background
I am new to django. I am trying to upload the file from client and save it.
For this pupose i have created below model.
from django.db import models
class UploadFile(models.Model):
uploadfile = models.FileField(upload_to='toProcess/')
I am using this model as below to save the file.
newfile = UploadFile(uploadfile = request.FILES['file'])
newfile.save()
It is saving file. But now I want to process saved file. In django, if the file with the same name alreday exists then it is adding some unique postfix to the original file name. I am happy with this approch and do not want to write a new method to create a unique filename.
Probelm-
How to get the new unique name calculated by django for a file?
Mean If I will upload a same file two times say "abc.pdf" then it will save the first uploaded file as "abc.pdf" and second uploaded file as "abc_somesuffix.pdf". How to know what is the name of saved file?
As far as I know, the filename is stored in the name attribute of the model field, in your case
newfile.uploadfile.name
and the path of the file is stored in
newfile.uploadfile.path
Please see the official Django docs for further reference, as well as many other SO Q&A (e.g. this one)
In case you want to adopt your own format for the file name you can specify a callable in the upload_to parameter of the model field, as explained here
Related
I am getting a file from the user and filter data from the model if the user uploaded file exists or not, but I am getting "None" while filtering.
from .models import SomeModel
uploaded_file = request.FILES.get('upload_file')
model_data = SomeModel.objects.filter(my_file=uploaded_file)
if model_data == None:
someModel.objects.create(my_file=uploaded_file)
else:
print('file already exist.')
print(model_data) # prints None
I have uploaded the same file too many times but it always creates new data in the model.
I know every time when uploaded file Django creates a new file in the media folder.
how can I filter if uploaded_file already exists in the model?
I would not suggest both of these approaches. Why?
Because multiple users may upload files with same name. Even same user can upload it multiple times with same name (So many users simply upload Untitles.xlsx.
Every time a file is uploaded, it is stored in a temporary location for the GET dictionary (try /tmp on linux). So your temporary name/location may be different if the same named file exists.
The intention might be to reupload the same file. In that case you might want to update.
So a safer option would be to get the checksum of the file (md5/sha1/sha256) and even store that in the database. That will tell you if the file is indeed the same.
You can check the currently uploaded file for checksum.
I recently came across this question and answer https://stackoverflow.com/a/44926557/12322095 regarding Flask file uploads.
This worked perfectly fine until I uploaded an image with the same name again. It didn't change the image or overlayed it.
My question here is what if a user uploads an image with the same name, is there any way we could show an error message or maybe automatically change the name to something else.
For automatic changing the name, I researched and it can be done via resolve_conflict but I couldn't understand how to implement it.
my code is ditto as the the reference
You need to create some kind of uniuqe ID to append to the filename, before saving the file.
This can be done with something like:
from uuid import uuid4
def make_unique(string):
ident = uuid4().__str__()
return f"{ident}-{string}"
Which adds an unique UUID to the beginning of the string:
>>> make_unique('something.txt')
'f374b80c-b09e-468f-adc6-3d9df175cee7-something.txt'
To use this in the upload code, just run the filename through that function before you save. Be sure to put the filename through the secure_filename function first though:
if file and allowed_file(file.filename):
original_filename = secure_filename(file.filename)
unique_filename = make_unique(original_filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], unique_filename))
Although this works for the purpose of avoiding duplicates, in a larger application you may wish to extend this approach.
If you store the values of original_filename and unique_filename in the database, then this allows you to do the following in the download route:
from flask import send_file
# ...
f = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
send_file(f, attachment_filename=original_filename)
This has the advantage that the file is stored on your server with a unique identifier, but the user would never know this, as the file is served back to them with the originally uploaded filename.
In fact you may wish to go further, and simply save the file on your end with the UUID string, rather than appending the filename; instead of using the make_unique function above, change that third line to:
unique_filename = uuid4().__str__()
This will still serve the file with the correct mimetype, as send_file guesses the mimetype based on the provided attachment_filename.
I am using pickle to store variables as:
pickle.dump(database,open("save.p", "wb"))
Here database is the variable (type dictionary) I am trying to store and save.p is the destination file.
Now I want to replace this as
pickle.dump(database,open("%s"%var, "wb"))
Here var is taken from stdin. Basically letting the user specify the file.
However just writing this does not do the trick.
The pickle documentation suggests that a new file for the given name will be created if not found in the specified path.
What I have tried:
pickle.dump(database,open(var, "wb"))
to see if it can access the variable directly and pickle.dump(database,open("%s", "wb")%var).
I am missing something very obvious here. But can't figure out what.
EDIT:
I suppose there was some error with permissions. I now create the file with write permissions before running the script and it works. Thank you everyone.
query = cgi.parse_multipart(handler.rfile, pdict)
I used function "parse_multipart" of module cgi to convert the form data, but i can not find the name of uploaded file in the content of variable "query"? where i can find the file's name. Thanks!
i want to upload zip folder from file input in form the i want to extract the contents of this uploaded zip folder,and store the contents (files)of this zip in the blobstore in order to download them after putting these files in one folder,but the problem is that i can't deal with the zip folder directly(to read it), i tried as this:
form = cgi.FieldStorage()
file_upload = form['file']
zip1=file_upload.filename
zipstream=StringIO.StringIO(zip1.read())
But the problem still that i can't read the zip as previous,also i tried to read zip folder directly like this:
z1=zipfile.ZipFile(zip1,"r")
But there was an error in this way.Please can any one help me.Thanks in advance.
Based on your comment, it sounds like you need to take a closer look at the cgi module documentation, which includes the following:
If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute...
This suggests that you need to modify your code to look something like:
form = cgi.FieldStorage()
file_upload = form['file']
z1 = zipfile.ZipFile(file_upload.file, 'r')
There are additional examples in the documentation.
You don't have to extract files from the zip in order to make them available for download - see this post for an example of serving direct from a zip. You can adapt that code if you want to extract the files and store them individually in the blobstore.