Django Upload文件获取全名和文件路径 [英] Django Upload file get full name and file path

查看:67
本文介绍了Django Upload文件获取全名和文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改云存储api,需要帮助,可以从Django的上传表单中获取完整的文件名.目前,我可以让用户从任何目录中选择文件"file.txt",并且可以通过

来获取名称.

 用于request.FILES.getlist('file')中的文件:打印文件名 

但是我想要完整的文件路径,更像是'home/user/documents/file.txt'

这可能吗,我怎么得到全名?我不希望实际上传文件,只是获取完整路径,以便可以使用Dropbox/Google Drive API.

供参考,这是我的表格:

  class UploadFileForm(forms.Form):folder_name = Forms.CharField(最大长度= 300)标题= Forms.CharField(max_length = 50)文件= Forms.FileField(label ='选择文件') 

谢谢.

解决方案

浏览器不会告诉您文件来自哪个目录.他们甚至没有将该信息提供给页面上的脚本.

用户上传文件时,您可能会知道:

  • 它的基本名称.
  • 它的大小.
  • 它的类型(通常是从扩展名中猜出来的).
  • 其修改时间.

这就是您将获得的一切,无论您是直接从浏览器的< input> 元素访问信息,还是等待它 POST 都可以./p>

您也可以反过来回答:如果您确实需要发布文件的完整路径,则需要开发一个客户端应用程序来发送该文件.只要它在网页沙箱外部运行,它就可以是独立的可执行文件,浏览器插件/应用程序或Java小程序.

我不使用保管箱,但我相信您需要下载并安装一些其他软件才能使用它.这样便可以访问文件的完整路径.

I'm tinkering around with cloud storage api's and need help getting the full file name from an upload form on Django. I currently can let the user choose a file, "file.txt" from any directory, and can get the name through

for file in request.FILES.getlist('file'):
    print file.name `

However I want the full file path, something more like 'home/user/documents/file.txt'

Is this possible and how would I get the full name? I'm not looking to actually upload the file, just get the full path so that I can utilize dropbox/google drive api.

For reference here is my form:

class UploadFileForm(forms.Form):
    folder_name = forms.CharField(max_length = 300)
    title = forms.CharField(max_length=50)
    file = forms.FileField(label='Select a file')

Thanks in advance.

解决方案

Browsers do not tell what directory the files come from. They do not even give that information to the scripts on the page.

When a user uploads a file, you may know:

  • Its basename.
  • Its size.
  • Its type (usually guessed from extension).
  • Its modification time.

This is all you will get, regardless of whether you access the information straight from the browser's <input> element, or wait for it to POST it.

You may also turn this answer the other way around: if you really need to be able to post the full path of the file, you need to develop a client-side application that will send it. It could be a standalone executable, a browser addon/app, or a Java applet, whatever as long as it runs outside of the webpage sandbox.

I do not use dropbox, but I believe you need to download and install some additional software to use it. That's how it would access the full path of your files.

这篇关于Django Upload文件获取全名和文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆