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是jQuery和flash结合的一个上传控件,source包中给出了upload.php,在apache中是可以很好的运行的。我的项目中是使用python,所以要写一个upload.py与之配合。python的upload脚本应该是比较容易的,参考了这里

参考了:

因为不清楚那个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:

Toggle line numbers
   1 import os
   2 import shutil
   3 
   4 tempFile = _Filedata.tmp_name
   5 targetPath = _folder + '/'
   6 targetFile = open(targetPath + os.path.basename(_Filedata.name), 'wb')
   7 shutil.copyfileobj(tempFile, targetFile)
   8 targetFile.close()
   9 
  10 print "1"

我查看了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。

解决

青椒土豆丝 <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.

服务端代码也很简单:

Toggle line numbers
   1 import os
   2 import shutil
   3 
   4 f = REQUEST['Filedata']
   5 file_name = REQUEST['Filename']
   6 file_path = REQUEST['folder']
   7 tmp_file = file_path + file_name
   8 out = open(tmp_file,'wb')
   9 shutil.copyfileobj(f.file, out, 1024*1024)
  10 out.close()
  11 print tmp_file


反馈

创建 by -- ZoomQuiet [2011-01-04 10:12:35]

MiscItems/2011-01-04 (last edited 2011-01-04 10:12:36 by ZoomQuiet)