#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0

    mod_python (psp) upload file? <SOLVED>


    Hi. I asked this in #python on freenode, and got some helpful info, but I think I'm below the level the users in there are used to, so I thought I would ask here.

    Basically, I have a plain html page that has a form. The user enters various data, all but one field of which are plain text. One field is a file field which the user can browse and select a file on their local machine.

    Here is a snippet of the html form:
    Code:
    <form action="missinglogic.psp" method="POST" enctype="multipart/form-data">
    Image:
    <input type="File" name="image"><br><br>
    Name: <input type="Text" name="name"><br><br>
    <center><input type="submit" value="Submit"></center>
    </form>
    As you can see, when the user presses the submit button, the form data is passed to the missinglogic.psp page. I would like to be able to use the file (image) in the html page that will be created by missinglogic.psp. I'm not sure if I have to copy it to my system first, but I think that is likely the case. Right now, I have the following code snippet in missinglogic.psp:

    Code:
    <%
    # Load the image
    
    imagefile = form['image'].file
    
    %>
    I'm not if this is correct, and if it is, how to use this image in the page I wish to generate with missinglogic.psp. I hope this makes sense, and thanks in advance for any suggestions you might have. Thanks also to those helping me on irc, if you also happen to frequent these forums.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I havnt done anything or use with PSP as of yet but you should be able to display the image simply by printing it . Give it a go and let us know,

    Mark.
    Last edited by netytan; September 5th, 2004 at 11:23 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    netytan,


    Thanks for your suggestion, I'm just not sure I completely understand what you mean. I tried the following, with the following errors:

    Code:
    <%
    # Load the image
    
    imagefile = form['image'].file
    %>
    <img src="<%=imagefile %>" width=600></img>
    When the page displayed, I got the missing image icon, and when I tried to just view the image, I got

    Code:
    The requested URL /<open file '<fdopen>', mode 'w+b' at 0x4e211220> was not found on this server
    I also tried this:

    Code:
    <%
    # Load the image
    
    imagefile = form['image'].file
    req.write(imagefile)
    %>
    and got

    Code:
    TypeError: argument 1 must be string or read-only buffer, not file
    So, it appears that imagefile is infact a file, but I'm still not able to display the image. Also, what is the actual filename on my system where this image is being temporarily stored? What if I wanted to save this image to a directory with a specific filename? Thanks again for you help. In case it helps, I think this is the php equivelent of what I'm trying to do. The php documentation is, obviously, quite better than the psp docs.

    http://ca3.php.net/features.file-upload
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    9
    Rep Power
    0
    Ok, with the help of some people in #python, in particular tunedude, I have a working solution. The following code will take the file object and save the file to a directory.

    Code:
    <%
    # Load the image
    if form['image'] != "":
            import os
            imagefile = form['image'].file
            imagefilename = form['image'].filename
            upload_dir = "/var/www/tempimages"
            fout = file(os.path.join(upload_dir, imagefilename), 'wb')
            while 1:
                    chunk = imagefile.read()
                    if not chunk: break
                    fout.write(chunk)
            fout.close()
    %>
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Its been a while but when i was first trying to upload images with CGI i found that all i had to do to display the image was print the_image. So saving wasn't required. Mind you this wasn't with mod_python or PSP.

    Note: I can't remember if it had to be printed inside an img tag. Also, you may need to escape the data. It would be easy enough to test but i dont have things set up for that right now.

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo