如何从request.FILES获取文件名?

时间:2022-01-06 05:11:47

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

如何从请求中获取文件名。文件在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']

4 个解决方案

#1


66  

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

#2


46  

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

#3


5  

NOTE if you are using python 3.x:

如果您正在使用python 3.x,请注意:

request.FILES is a multivalue dictionary like object that keeps the files uploaded through an upload file button. Say in your html code the name of the button (type="file") is "myfile" so "myfile" will be the key in this dictionary. If you uploaded one file, then the value for this key will be only one and if you uploaded multiple files, then you will have multiple values for that specific key. If you use request.FILES['myfile'] you will get the first or last value (I cannot say for sure). This is fine if you only uploaded one file, but if you want to get all files you should do this:

请求。文件是一个多值字典类对象,通过上传文件按钮来保存文件。在html代码中,按钮的名称(type="file")是"myfile",因此"myfile"将是这个字典中的键。如果您上传了一个文件,那么这个键的值将只有一个,如果您上传了多个文件,那么您将为这个特定的键拥有多个值。如果您使用请求。文件['myfile']您将得到第一个或最后一个值(我不能肯定地说)。如果你只上传了一个文件,这是可以的,但是如果你想获得所有的文件,你应该这样做:

list=[] #myfile is the key of a multi value dictionary, values are the uploaded files
for f in request.FILES.getlist('myfile'): #myfile is the name of your html file button
    filename = f.name
    list.append(filename)

of course one can squeeze the whole thing in one line, but this is easy to understand

当然,一个人可以把整件事压缩成一行,但这很容易理解

#4


3  

The answer may be outdated, since there is a name property on the UploadedFile class. See: Uploaded Files and Upload Handlers (Django docs). So, if you bind your form with a FileField correctly, the access should be as easy as:

答案可能是过时的,因为在UploadedFile类中有一个name属性。参见:上传文件和上传处理程序(Django文档)。因此,如果您正确地将窗体与FileField绑定,那么访问应该简单到:

if form.is_valid():
    form.cleaned_data['my_file'].name

#1


66  

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

#2


46  

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

#3


5  

NOTE if you are using python 3.x:

如果您正在使用python 3.x,请注意:

request.FILES is a multivalue dictionary like object that keeps the files uploaded through an upload file button. Say in your html code the name of the button (type="file") is "myfile" so "myfile" will be the key in this dictionary. If you uploaded one file, then the value for this key will be only one and if you uploaded multiple files, then you will have multiple values for that specific key. If you use request.FILES['myfile'] you will get the first or last value (I cannot say for sure). This is fine if you only uploaded one file, but if you want to get all files you should do this:

请求。文件是一个多值字典类对象,通过上传文件按钮来保存文件。在html代码中,按钮的名称(type="file")是"myfile",因此"myfile"将是这个字典中的键。如果您上传了一个文件,那么这个键的值将只有一个,如果您上传了多个文件,那么您将为这个特定的键拥有多个值。如果您使用请求。文件['myfile']您将得到第一个或最后一个值(我不能肯定地说)。如果你只上传了一个文件,这是可以的,但是如果你想获得所有的文件,你应该这样做:

list=[] #myfile is the key of a multi value dictionary, values are the uploaded files
for f in request.FILES.getlist('myfile'): #myfile is the name of your html file button
    filename = f.name
    list.append(filename)

of course one can squeeze the whole thing in one line, but this is easy to understand

当然,一个人可以把整件事压缩成一行,但这很容易理解

#4


3  

The answer may be outdated, since there is a name property on the UploadedFile class. See: Uploaded Files and Upload Handlers (Django docs). So, if you bind your form with a FileField correctly, the access should be as easy as:

答案可能是过时的,因为在UploadedFile类中有一个name属性。参见:上传文件和上传处理程序(Django文档)。因此,如果您正确地将窗体与FileField绑定,那么访问应该简单到:

if form.is_valid():
    form.cleaned_data['my_file'].name