How to upload an image from React Native to Web2py?

89 views
Skip to first unread message

Mika Sjöman

unread,
Mar 12, 2017, 4:40:12 PM3/12/17
to web2py-users
Hi

I am trying to upload an image from a React Native app to web2py. I am using a plugin called react-native-uploader, and when i upload the image I get a storage object when I return the request vars using the below code:

def upload:
    return response.json({'vars': str(request.vars)})

When I print out the response in my Chrome dev tools, I get the following printout:

    1. Object
      1. vars:"<Storage {'file[]': FieldStorage('file[]', 'mikasworld.jpg', '\xff\xd8\xff\xe0\x00\x10JFIx00\x01\x01\x00\x00\x00\x00\x00\x00\x00\xff\xe1\x00\x8cExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x05\x01\x12\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01\x1a\x00\x05\x00\x00\x00\x01\x00\x00\x00J\x01\x1b\x00\x05\x00\x00\x00\x01\x00\x00\x00R\x01(\x00\x03\x00\x00\x00\x01\x00\x02\x00\x00\x87i\x00\x04\x00\x00\x00\x01\x00\x00\x00Z\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x03\xa0\x01\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\xa0\x02\x00\x04\x00\x00\x00\x01\x00\x00\x05\xa0\xa0\x03\x00\x04\x00\x00\x00\x01\x00\x00\n\x00\x00\x00\x00\x00\xff\xed\x008Photoshop 3.0\x008BIM\x04\x04\x00\x00\x00\x00\x00\x008BIM\x04%\x00\x00\x00\x00\x00\x10\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~\xff\xc0\x00\x11\x08\n\x00\x05\xa0\x03\x01\x11\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02w\x00\x01\x02\x03\x11\x04\x05!1\x06\x12AQ\x07aq\x13"2\x81\x08\x14B\x91\xa1\xb1\xc1\t#3R\xf0\x15br\xd1\n\x16$4\xe1%\xf1\x17\x18\x19\x1a&\'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xdb\x00C\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\xff\xdb\x00C\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\xff\xdd\x00\x04\x00\xb4\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd0\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd1\xfc\x9f\xaf…n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd3\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd4\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd5\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd6\xfc\x9f\xaf\xe1\xf3\xfc_\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x02\x80\n\x00(\x00\xa0\x0f\xff\xd9')}>"
      2. __proto__:

Now I want to do two thing:
1. Move the file to a location accessible from web2py
2. send back a modified name of the file to the React Native client in a JSON object.

I have searched like crazy in the Google Group but also the web, but I cant find anything on how to do this. Any ideas?

Cheers



Leonel Câmara

unread,
Mar 13, 2017, 12:42:07 PM3/13/17
to web2py-users
Something like this should work (I haven't tested it).

# Model
db
.define_table('my_uploads',
   
Field('upload_field', 'upload')
)


# Controller
def upload():
   
""" Store the upload and return an URL to see the new file """
    stored_name
= db.my_uploads.upload_field.store(request.vars['file[]'])  # Note that Field.store works fine with cgi.FieldStorage
    db
.my_uploads.insert(upload_field=stored_name)
   
return URL('default', 'download', args=stored_name, scheme=True, host=True)




Mika Sjöman

unread,
Mar 13, 2017, 3:10:32 PM3/13/17
to web2py-users
Hi and thanks :)

Ill try that later. But I need to keep the image outside of the db (not in a blob field), and only keep a reference to the location of the image on the disk. I am going to run a python script on the image I am uploading to do OCR analysis triggered by the Scheduler, so I need the image on the disk. How can I move the image to somewhere on the disk instead of saving it in the db?

Cheers

Dave S

unread,
Mar 13, 2017, 3:26:51 PM3/13/17
to web2py-users


On Monday, March 13, 2017 at 12:10:32 PM UTC-7, Mika Sjöman wrote:
Hi and thanks :)

Ill try that later. But I need to keep the image outside of the db (not in a blob field), and only keep a reference to the location of the image on the disk. I am going to run a python script on the image I am uploading to do OCR analysis triggered by the Scheduler, so I need the image on the disk. How can I move the image to somewhere on the disk instead of saving it in the db?

Cheers


We may need to know more about your React Native tool.  If you were using the Web2Py upload support, it would (unless told otherwise) end up in a file, and whether you used defaults or specified a location you'd know where that file was.  It might help to look at the network traffic for the uploads, and see what URL the post is using.  (The browser's DevTools console should have a Network tab.  You can get to the console via right-click->inspect element on the page you're using.)

I've had the briefest intro to React, so I don't know much about your component.

Is this a mobile app, BTW?

/dps

 

Dave S

unread,
Mar 13, 2017, 3:31:00 PM3/13/17
to web2py-users


On Monday, March 13, 2017 at 12:26:51 PM UTC-7, Dave S wrote:


On Monday, March 13, 2017 at 12:10:32 PM UTC-7, Mika Sjöman wrote:
Hi and thanks :)

Ill try that later. But I need to keep the image outside of the db (not in a blob field), and only keep a reference to the location of the image on the disk. I am going to run a python script on the image I am uploading to do OCR analysis triggered by the Scheduler, so I need the image on the disk. How can I move the image to somewhere on the disk instead of saving it in the db?

Cheers


We may need to know more about your React Native tool.

If it is putting it in a DB that you know how to talk to, you should be able to extract it and stream it into a file local to the server.  The web2py download function (applications/welcome/controllers/default.py) differs only in making the stream go to the requester.

/dps

 

Leonel Câmara

unread,
Mar 13, 2017, 4:23:27 PM3/13/17
to web...@googlegroups.com
Sorry I assumed you wanted it in the DB you don't want it in the DB. You can just manipulate the file directly and save it however you want, you have a cgi.FieldStorage in request.vars['file[]'] you can use it to access the uploaded file.

up_file = request.vars['file[]'] 
up_file.file  # will have a file object you can use to read the data and write it to disk
up_file.filename # wil have the uploaded file filename

edit Note that in my previous example the files were not being stored in a blob but in the filesystem

Mika Sjöman

unread,
Mar 13, 2017, 5:46:09 PM3/13/17
to web...@googlegroups.com
Hi

Yes it is a React Native mobile app :)  Since it is using native iOS components, it is not possible to right-click and look at the DOM (it doesn't have one because it is not a web page rendered inside a web-view). Neither can I look at the network tab, because I only get a bridge to the console by using the React Native debugger. I guess it is using the javascript bridge from native iOS/Andriod back to react-native to send messages back and forth. I can't monitor network traffic by using the Chrome Webtools. 

The strange thing is that I do get a response using my code that there is a storage object, but there isnt any file on my disk with the name shown in the response. I also thought that since I am getting a 200 response and I am getting a Storage object, that it would be saved to disk - but that doesn't seem to be the case :( So it... confuses me... 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/7yCg0huUwXE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-- 
Mvh/Sincerely yours

Mika Sjöman

Homepage: http://www.omfriheten.com, Learn Chinese at our website: http://www.dominochinese.com 


Mika Sjöman

unread,
Mar 13, 2017, 5:51:08 PM3/13/17
to web...@googlegroups.com
I tried that (up_file = request.vars['file[]']), but I still just get the String printed out in my console:

"FieldStorage('file[]', 'mikasworld.jpg', '\xff\...base64 representation of the image.

The strange thing is that the upload seems to go all right, but it never saves the file. If I remember right, uploaded files just appears in the /upload folder once complete, but this doesn't happen here. Strange....
I am getting this ouput in my console, so for sure it upload the file and given the request.vars response, it seems like the server is accepting the upload but not saving the file. This is my output during the upload:

upload progress: 19.57139819860458%
ImageViewer.js:75 upload progress: 39.14279639720917%
ImageViewer.js:75 upload progress: 58.71419310569763%
ImageViewer.js:75 upload progress: 78.28559279441833%
ImageViewer.js:75 upload progress: 97.85698652267456%
ImageViewer.js:75 upload progress: 100%

Any ideas on why I get a FieldStorage object but not a saved file to disk on the server? Is there any step I need to take after the file has been uploaded that finally saves the file to disk?


On Mon, Mar 13, 2017 at 9:23 PM, Leonel Câmara <leonel...@gmail.com> wrote:
Sorry I assumed you wanted it in the DB you don't want it in the DB. You can just manipulate the file directly and save it whoever you want, you have a cgi.FieldStorage in request.vars['file[]'] you can use it to access the uploaded file.

up_file = request.vars['file[]'] 
up_file.file  # will have a file object you can use to read the data and write it to disk
up_file.filename # wil have the uploaded file filename

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/7yCg0huUwXE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
Mar 13, 2017, 7:18:51 PM3/13/17
to web2py-users


On Monday, March 13, 2017 at 2:51:08 PM UTC-7, Mika Sjöman wrote:
I tried that (up_file = request.vars['file[]']), but I still just get the String printed out in my console:

"FieldStorage('file[]', 'mikasworld.jpg', '\xff\...base64 representation of the image.

 
After you got that, did you print up_file.file and up_file.filename?

The strange thing is that the upload seems to go all right, but it never saves the file. If I remember right, uploaded files just appears in the /upload folder once complete, but this doesn't happen here. Strange....

Remember that web2py likes to munge the names of the uploaded files, both to avoid dangerous names and to limit
 
I am getting this ouput in my console, so for sure it upload the file and given the request.vars response, it seems like the server is accepting the upload but not saving the file. This is my output during the upload:

upload progress: 19.57139819860458%
ImageViewer.js:75 upload progress: 39.14279639720917%
ImageViewer.js:75 upload progress: 58.71419310569763%
ImageViewer.js:75 upload progress: 78.28559279441833%
ImageViewer.js:75 upload progress: 97.85698652267456%
ImageViewer.js:75 upload progress: 100%

Any ideas on why I get a FieldStorage object but not a saved file to disk on the server? Is there any step I need to take after the file has been uploaded that finally saves the file to disk?

On Mon, Mar 13, 2017 at 9:23 PM, Leonel Câmara <leonel...@gmail.com> wrote:
Sorry I assumed you wanted it in the DB you don't want it in the DB. You can just manipulate the file directly and save it whoever you want, you have a cgi.FieldStorage in request.vars['file[]'] you can use it to access the uploaded file.

up_file = request.vars['file[]'] 
up_file.file  # will have a file object you can use to read the data and write it to disk
up_file.filename # wil have the uploaded file filename
[...]
--
-- 
Mvh/Sincerely yours

Mika Sjöman

Homepage: http://www.omfriheten.com, Learn Chinese at our website: http://www.dominochinese.com 



[google groups doesn't understand sig delimiters.  Pity]

/dps
 

Mika Sjöman

unread,
Mar 14, 2017, 1:56:49 AM3/14/17
to web...@googlegroups.com
Yes I did both but I kept getting the same output no matter what I did. 

//Cheers 

--

Leonel Câmara

unread,
Mar 14, 2017, 6:20:16 AM3/14/17
to web2py-users
Mika just because you've uploaded the file it doesn't mean web2py has saved it to disk. You always get that string because you are printing it which calls the __str__ method in stuff which returns a string.  

The file is not anywhere on disk when it comes to web2py until you actually write it there. The file is in the request in a cgi.FieldStorage object.

Mika Sjöman

unread,
Mar 22, 2017, 3:51:41 PM3/22/17
to web2py-users
Yeah, I am back at it now. But I still dont seem to be able to save the object. Any ideas on how to save this object? I have tried and tried but I am just not able to save it :(

// Mika

Dave S

unread,
Mar 22, 2017, 4:01:45 PM3/22/17
to web2py-users


On Monday, March 13, 2017 at 10:56:49 PM UTC-7, Mika Sjöman wrote:
Yes I did both but I kept getting the same output no matter what I did. 

//Cheers 


I think you need to copy the field store to a file.  That is, you do a normal python open for a file that's going to be where you want it, and it's write method needs to suck stuff out of the field store.  I don't know enough about the latter to know if that's store.stream() or store.read().

After the store has been poured into the file, close the file.

/dps

 

To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Dave S

unread,
Mar 22, 2017, 4:09:30 PM3/22/17
to web...@googlegroups.com


On Wednesday, March 22, 2017 at 1:01:45 PM UTC-7, Dave S wrote:


On Monday, March 13, 2017 at 10:56:49 PM UTC-7, Mika Sjöman wrote:
Yes I did both but I kept getting the same output no matter what I did. 

//Cheers 


I think you need to copy the field store to a file.  That is, you do a normal python open for a file that's going to be where you want it, and it's write method needs to suck stuff out of the field store.  I don't know enough about the latter to know if that's store.stream() or store.read().

After the store has been poured into the file, close the file.

/dps


docs.python.org says you either read fileitem.value / fileitem.getvalue()) , getting one long string, or you can do something like

 if fileitem.file:
   linecount
= 0
   
while 1:
      line
= fileitem.file.readline()
     
if not line: break
      linecount
= linecount + 1



Reply all
Reply to author
Forward
0 new messages