Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the file name from request.FILES?

How can I get the file name from request.FILES in Django?

def upload(request):     if request.method == 'POST':         form = UploadForm(request.POST, request.FILES)         if form.is_valid():             upload = Upload()             upload.timestamp = datetime.datetime.now()             save_instance(form, upload) 

I tried using this but it did not work:

  if 'filename'  in request.FILES:          filename = request.FILES['filename'] 
like image 350
Pol Avatar asked Jun 24 '10 16:06

Pol


People also ask

How do I find the upload file name?

Try document. getElementById("FileUpload1"). value this value should have a path for a file to be uploaded, just strip all dirs from that value and you will have file name.

What is a request file?

With the file request feature in OneDrive, you can choose a folder where others can upload files using a link that you send them. People you request files from can only upload files; they can't see the content of the folder, edit, delete, or download files, or even see who else has uploaded files.

How do I find the path of an uploaded file in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.


2 Answers

request.FILES['filename'].name 

From the request documentation.

If you don't know the key, you can iterate over the files:

for filename, file in request.FILES.iteritems():     name = request.FILES[filename].name 
like image 117
mipadi Avatar answered Oct 02 '22 17:10

mipadi


file = request.FILES['filename'] file.name           # Gives name file.content_type   # Gives Content type text/html etc file.size           # Gives file's size in byte file.read()         # Reads file 
like image 35
ecabuk Avatar answered Oct 02 '22 17:10

ecabuk