Contents
karrigell配合Flash 文件上传组件的事儿
问题
青椒土豆丝 <yl.bobby@gmail.com> 回复 python-cn@googlegroups.com 发送至 "python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)" <python-cn@googlegroups.com> 日期 2010年11月12日 下午12:54 主题 [CPyUG] Karrigell中对文件上传的处理
上传控件:uploadify-2.1 http://www.uploadify.com/
- Server: ubuntu-10.04-server-i386
- Karrigell版本:Karrigell-3.1.1
uploadify是jQuery和flash结合的一个上传控件,source包中给出了upload.php,在apache中是可以很好的运行的。我的项目中是使用python,所以要写一个upload.py与之配合。python的upload脚本应该是比较容易的,参考了这里
http://karrigell.sourceforge.net/en/programming.htm 的6.4节,一个最基本的上传脚本。
- 但无法上传,页面上的flash会崩溃,不知道有没有走到upload.py。在其中用open/write的方式debug(不好意思,我不知道还有什么好的方式可以debug),在一进脚本的地方就打“Hello”,在debug文件中打不出来任何东西,只有很诡异的1%的几率会打出信息。
参考了:
- Limodou 05/07年写的Karrigell学习的相关blog,也没有解决。
因为不清楚那个flash控件是封装好的,不清楚传递的参数,只好参考upload.php,改写了代码如下:
upload.php:
<?php // JQuery File Upload Plugin v1.4.1 by RonnieSan - (C)2009 Ronnie Garcia if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/'; $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; // Uncomment the following line if you want to make the directory if it doesn't exist // mkdir(str_replace('//','/',$targetPath), 0755, true); move_uploaded_file($tempFile,$targetFile); } echo '1'; ?>
upload.py:
我查看了Karrigell文档,有这样一条信息:
10. Files and directories In a shared environment like a web server, requests can be managed in different threads or processes. When a script is executed, it is not reliable to set the current directory (by os.chdir) to the folder where the script stands, because another thread could modify it before the script is finished So you must be careful if a script has to open files : * you can provide a relative path to the built-in functions open() or file() : they are modified by Karrigell so that relative paths are translated to absolute path, relative to the script folder * you can use the built-in value CWD which provides the absolute path of the script directory * or you can use the built-in function REL() which converts a relative path to an absolute path, relative to the script directory
但我没有把上传目录和脚本放在同一目录下,而且,将上传目录targetPath写死,也没有用。
我的想法是,uploadify控件应该没有问题,而且是封装好的,又是flash,不好debug。
- 问题应该还是在python脚本上,用简单几句 php脚本解决的问题,python脚本应该也可以。请求帮助,谢谢!
解决
青椒土豆丝 <yl.bobby@gmail.com> 回复 python-cn@googlegroups.com 发送至 "python-cn`CPyUG`华蟒用户组(中文Python技术邮件列表)" <python-cn@googlegroups.com> 日期 2011年1月4日 下午6:00
这个问题N久了,后来停在那,先做其他功能了。后来模仿uploadify写了个jQuery插件,但上传大文件的时候又遇到问题,继续捣鼓。最终发现了问题的关键,今天来做个了结。
关键的问题是Karrigell中,没有设置tempdir
tempfile.tempdir¶ When set to a value other than None, this variable defines the default value for the dir argument to all the functions defined in this module. If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in. The list is: The directory named by the TMPDIR environment variable. The directory named by the TEMP environment variable. The directory named by the TMP environment variable. A platform-specific location: On RiscOS, the directory named by the Wimp$ScrapDir environment variable. On Windows, the directories C:\TEMP, C:\TMP, \TEMP, and \TMP, in that order. On all other platforms, the directories /tmp, /var/tmp, and /usr/tmp, in that order. As a last resort, the current working directory.
添加:export TMPDIR=/raid/data/tmp
- 或者直接在Async_core.py中,设置
tempfile.tempdir= '/raid/data/tmp'
- 再以Karrigell_async.py模式启动,就可以使用uploadify插件。
服务端代码也很简单:
反馈
创建 by -- ZoomQuiet [2011-01-04 10:12:35]