【问题标题】:Python Image Uploading with AjaxUpload使用 AjaxUpload 上传 Python 图像
【发布时间】:2010-12-03 19:53:19
【问题描述】:

我正在尝试将 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)

【问题讨论】:

    标签: python pylons python-imaging-library image-uploading ajax-upload


    【解决方案1】:
    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.
    

    【讨论】:

      【解决方案2】:

      我正在与 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)
      

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

      【讨论】:

        【解决方案3】:

        在 django 中,你可以使用:

        request.FILES['file']
        

        代替:

        request.POST['file']
        

        我不知道如何在 pylons 中做...也许是同一个概念..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-03-23
          • 2014-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-24
          相关资源
          最近更新 更多