Python 使用 AjaxUpload 上传图像

发布于 2024-08-07 06:32:08 字数 438 浏览 4 评论 0原文

我正在尝试将 AjaxUpload 与 Python 一起使用: http://valums.com/ajax-upload/

我想知道如何访问使用Python上传文件。在网站上,它说:

* PHP: $_FILES['userfile']
* Rails: params[:userfile]

Python 的语法是什么?

request.params['userfile'] 似乎不起作用。

提前致谢!这是我当前的代码(使用作为图像导入的 PIL)

im = Image.open(request.params['myFile'].file)

I'm trying to use AjaxUpload with Python:
http://valums.com/ajax-upload/

I would like to know how to access the uploaded file with Python. On the web site, it says:

* PHP: $_FILES['userfile']
* Rails: params[:userfile]

What is the Syntax for Python?

request.params['userfile'] doesn't seem to work.

Thanks in advance! Here is my current code (using PIL imported as Image)

im = Image.open(request.params['myFile'].file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-08-14 06:32:08
import cgi

#This will give you the data of the file,
# but won't give you the filename, unfortunately.
# For that you have to do some other trick.
file_data = cgi.FieldStorage.getfirst('file')

#<IGNORE if you're not using mod_python>

#(If you're using mod_python you can also get the Request object
# by passing 'req' to the relevant function in 'index.py', like "def func(req):"
# Then you access it with req.form.getfirst('file') instead. NOTE that the
# first method will work even when using mod_python, but the first FieldStorage
# object called is the only one with relevant data, so if you pass 'req' to the
# function you have to use the method that uses 'req'.)

#</IGNORE>

#Then you can write it to a file like so...
file = open('example_filename.wtvr','w')#'w' is for 'write'
file.write(file_data)
file.close()

#Then access it like so...
file = open('example_filename.wtvr','r')#'r' is for 'read'

#And use file.read() or whatever else to do what you want.
import cgi

#This will give you the data of the file,
# but won't give you the filename, unfortunately.
# For that you have to do some other trick.
file_data = cgi.FieldStorage.getfirst('file')

#<IGNORE if you're not using mod_python>

#(If you're using mod_python you can also get the Request object
# by passing 'req' to the relevant function in 'index.py', like "def func(req):"
# Then you access it with req.form.getfirst('file') instead. NOTE that the
# first method will work even when using mod_python, but the first FieldStorage
# object called is the only one with relevant data, so if you pass 'req' to the
# function you have to use the method that uses 'req'.)

#</IGNORE>

#Then you can write it to a file like so...
file = open('example_filename.wtvr','w')#'w' is for 'write'
file.write(file_data)
file.close()

#Then access it like so...
file = open('example_filename.wtvr','r')#'r' is for 'read'

#And use file.read() or whatever else to do what you want.
初心 2024-08-14 06:32:08

我正在与 Pyramid 合作,我也在尝试做同样的事情。一段时间后我想出了这个解决方案。

from cStringIO import StringIO
from cgi import FieldStorage

fs = FieldStorage(fp=request['wsgi.input'], environ=request)
f = StringIO(fs.value)

im = Image.open(f)

我不确定这是否是“正确的”,但它似乎有效。

I'm working with Pyramid, and I was trying to do the same thing. After some time I came up with this solution.

from cStringIO import StringIO
from cgi import FieldStorage

fs = FieldStorage(fp=request['wsgi.input'], environ=request)
f = StringIO(fs.value)

im = Image.open(f)

I'm not sure if it's the "right" one, but it seems to work.

落在眉间の轻吻 2024-08-14 06:32:08

在django中,你可以使用:

request.FILES['file']

而不是:

request.POST['file']

我不知道如何在pylons中做...也许这是相同的概念..

in django, you can use:

request.FILES['file']

instead of:

request.POST['file']

i did not know how to do in pylons...maybe it is the same concept..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文