[web2py] Re: uploading images without page refresh
i think you can achieve it using jquery (onsubmit) and ajax. please check the web2py book about jquery and ajax. ref: http://web2py.com/books/default/chapter/29/11/jquery-and-ajax best regards, stifan -- 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 the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: uploading images without page refresh
I did try it based on the example in the book. What must the return in the controller be? I want a delete button next to the image in the views to delete that specific image without refreshing as well. On Sunday, November 10, 2013 4:06:09 PM UTC+5:30, 黄祥 wrote: i think you can achieve it using jquery (onsubmit) and ajax. please check the web2py book about jquery and ajax. ref: http://web2py.com/books/default/chapter/29/11/jquery-and-ajax best regards, stifan -- 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 the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: uploading images without page refresh
for a delete button next to the image in the views, i think you can achieve it using A built in helpers. e.g. not tested in views for delete button {{=SPAN(A(I(_class='icon-remove'), callback=URL('function_callback', args=image.id)), delete='tr', _title='Remove Image' % p.name, _class='btn btn-danger'))}} in view for ajax form form id=myform input type=file name=image id=imagebr input type=submit / /form script jQuery('#myform').submit(function() { ajax('{{=URL('function_callback')}}', ['image'], ':eval'); return false; }); /script and in controllers : def function_callback(): record = db.person(request.args(0)) or redirect(URL('index')) form = SQLFORM(db.image, record) if form.accepts(request, formname = None): return DIV(Image uploaded) elif form.errors: return TABLE(*[TR(k, v) for k, v in form.errors.items()]) ref : http://web2py.com/books/default/chapter/29/05/the-views#Built-in-helpers best regards, stifan -- 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 the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: uploading images without page refresh
you can't upload files with a simple ajax POST, and that's why web2py doesn't support it right out of the box. There is a zillion of javascript plugin to make file uploads working, but they needs adjustment on the receiving part too and careful thinking about browser's support of the needed features to make it work. On web2pyslices.com you can find several implementations. -- 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 the Google Groups web2py-users group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[web2py] Re: Uploading images
Hello Alan, Thank you very much for your answer. Finally, based on your note ( ... renames uploaded files (to prevent directory traversal attacks) ...), in the difficulty of the changes, and taking into account that the rename is not very necessary in fact, I've decided not to change the name of the uploaded files. Now I upload and retrieve the images with the code Massimo gave me and the download() function you say. Thank you very much for the help! El lunes, 7 de enero de 2013 02:19:03 UTC+1, Alan Etkin escribió: This makes me think, is there any way so the stored file is accessed directly via URL, something like http://mysite.com/myapp/uploads/myfilename.jpg? This should customize the uploaded files. - Retrieve the file db record - Change the upload field to whatever name you need - Rename the file with the Python os library The name change is a security measure: ... renames uploaded files (to prevent directory traversal attacks) ... Note that the download() function restores the original filename on client downloads --
[web2py] Re: Uploading images
Hello Massimo, First of all thank you very much for your help. I've done what you told me, to be exact: file_name = auth.user.username+'.jpg' print file_name row.update(avatar=db.auth_user.avatar.store(request.post_vars['upload_field' ],filename=file_name)) Running this, it seems that everything went well. The output is: myuser.jpg In the database I get this: auth_user.id auth_user.username ... auth_user.avatar 1myuser ... *file* file is a link pointing to: https://192.168.1.131:8000/myapp/appadmin/download/db/auth_user.avatar.a633aec9680b76a0.6d696e6f6d6272652e6a7067.jpg And the content of the file is ok, is the new avatar updated to the database. But, do you know why the name is not myuser.jpg? I would like to call this link directly from the mobile, and it seems that the name is generated randomly. And, looking at the URL /donwload/db/..., is the image stored in the server or in the database? I've tried to find it in my server without success. Thank you very much and kind regards! El sábado, 5 de enero de 2013 01:50:00 UTC+1, Massimo Di Pierro escribió: yes row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'],filename='yourname.ext')) On Friday, 4 January 2013 00:36:31 UTC-6, Wonton wrote: Hello Massimo! I bypass the form.proccess because this is a web service used by an iOS app. I do all frontend stuff in iOS and call this web service from the device. And regarding to change the name of the file, is it possible? kind regards! El viernes, 4 de enero de 2013 04:11:16 UTC+1, Massimo Di Pierro escribió: You can do row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --
[web2py] Re: Uploading images
Regarding to my second question, I've found the image file stored in my server under applications/myapp/uploads, but its name is not myuser.jpg but auth_user.avatar.8d275a959267dd9b.612e6a7067.jpg. El lunes, 7 de enero de 2013 00:26:03 UTC+1, Wonton escribió: Hello Massimo, First of all thank you very much for your help. I've done what you told me, to be exact: file_name = auth.user.username+'.jpg' print file_name row.update(avatar=db.auth_user.avatar.store(request.post_vars[ 'upload_field'],filename=file_name)) Running this, it seems that everything went well. The output is: myuser.jpg In the database I get this: auth_user.id auth_user.username ... auth_user.avatar 1myuser ... *file* file is a link pointing to: https://192.168.1.131:8000/myapp/appadmin/download/db/auth_user.avatar.a633aec9680b76a0.6d696e6f6d6272652e6a7067.jpg And the content of the file is ok, is the new avatar updated to the database. But, do you know why the name is not myuser.jpg? I would like to call this link directly from the mobile, and it seems that the name is generated randomly. And, looking at the URL /donwload/db/..., is the image stored in the server or in the database? I've tried to find it in my server without success. Thank you very much and kind regards! El sábado, 5 de enero de 2013 01:50:00 UTC+1, Massimo Di Pierro escribió: yes row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'],filename='yourname.ext')) On Friday, 4 January 2013 00:36:31 UTC-6, Wonton wrote: Hello Massimo! I bypass the form.proccess because this is a web service used by an iOS app. I do all frontend stuff in iOS and call this web service from the device. And regarding to change the name of the file, is it possible? kind regards! El viernes, 4 de enero de 2013 04:11:16 UTC+1, Massimo Di Pierro escribió: You can do row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --
[web2py] Re: Uploading images
Well, once again sorry for answering myself, but, as I'm trying to understand this and read posts in Internet I find out more things, for example, that in the official book this is said: It uses the filename to determine the extension (type) of the file, creates a new temp name for the file (according to web2py upload mechanism) and loads the file content in this new temp file (under the uploads folder unless specified otherwise). It returns the new temp name, which is then stored in the image field of the db.myfile table., so It's clear to me that it's normal that my image is stored with a strange name, it's its temp name. This makes me think, is there any way so the stored file is accessed directly via URL, something like http://mysite.com/myapp/uploads/myfilename.jpg? El lunes, 7 de enero de 2013 00:36:00 UTC+1, Wonton escribió: Regarding to my second question, I've found the image file stored in my server under applications/myapp/uploads, but its name is not myuser.jpg but auth_user.avatar.8d275a959267dd9b.612e6a7067.jpg. El lunes, 7 de enero de 2013 00:26:03 UTC+1, Wonton escribió: Hello Massimo, First of all thank you very much for your help. I've done what you told me, to be exact: file_name = auth.user.username+'.jpg' print file_name row.update(avatar=db.auth_user.avatar.store(request.post_vars[ 'upload_field'],filename=file_name)) Running this, it seems that everything went well. The output is: myuser.jpg In the database I get this: auth_user.id auth_user.username ... auth_user.avatar 1myuser ... *file* file is a link pointing to: https://192.168.1.131:8000/myapp/appadmin/download/db/auth_user.avatar.a633aec9680b76a0.6d696e6f6d6272652e6a7067.jpg And the content of the file is ok, is the new avatar updated to the database. But, do you know why the name is not myuser.jpg? I would like to call this link directly from the mobile, and it seems that the name is generated randomly. And, looking at the URL /donwload/db/..., is the image stored in the server or in the database? I've tried to find it in my server without success. Thank you very much and kind regards! El sábado, 5 de enero de 2013 01:50:00 UTC+1, Massimo Di Pierro escribió: yes row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'],filename='yourname.ext')) On Friday, 4 January 2013 00:36:31 UTC-6, Wonton wrote: Hello Massimo! I bypass the form.proccess because this is a web service used by an iOS app. I do all frontend stuff in iOS and call this web service from the device. And regarding to change the name of the file, is it possible? kind regards! El viernes, 4 de enero de 2013 04:11:16 UTC+1, Massimo Di Pierro escribió: You can do row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --
[web2py] Re: Uploading images
This makes me think, is there any way so the stored file is accessed directly via URL, something like http://mysite.com/myapp/uploads/myfilename.jpg? This should customize the uploaded files. - Retrieve the file db record - Change the upload field to whatever name you need - Rename the file with the Python os library The name change is a security measure: ... renames uploaded files (to prevent directory traversal attacks) ... Note that the download() function restores the original filename on client downloads --
[web2py] Re: Uploading images
yes row.update(avatar=db.tablename.avatar.store(request.post_vars['upload_field' ],filename='yourname.ext')) On Friday, 4 January 2013 00:36:31 UTC-6, Wonton wrote: Hello Massimo! I bypass the form.proccess because this is a web service used by an iOS app. I do all frontend stuff in iOS and call this web service from the device. And regarding to change the name of the file, is it possible? kind regards! El viernes, 4 de enero de 2013 04:11:16 UTC+1, Massimo Di Pierro escribió: You can do row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --
[web2py] Re: Uploading images
You can do row.update(avatar=db.tablename.avatar.store(request.post_vars['upload_field' ])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --
[web2py] Re: Uploading images
Hello Massimo! I bypass the form.proccess because this is a web service used by an iOS app. I do all frontend stuff in iOS and call this web service from the device. And regarding to change the name of the file, is it possible? kind regards! El viernes, 4 de enero de 2013 04:11:16 UTC+1, Massimo Di Pierro escribió: You can do row.update(avatar=db.tablename.avatar.store(request.post_vars[ 'upload_field'])) by why bypess form.process()? it does it for you. On Thursday, 3 January 2013 15:57:24 UTC-6, Wonton wrote: Hello! I'm trying to implement uploading user's avatar. I've tried to follow the image blog example from the book and some of the posts related to this issue in this forum and I'm still a bit confused and I don't know if I could do certain things or how to do them. What I would like to achieve is this: -Upload the avatar and store it in the file system. The link to the file will be stored in the database. As far as I know this is possible with Field('avatar', 'upload'). But, my first doubt: could I store the image file with a custom name, for example auth_user.username.jpg? I see that the file has a strange name when it's stored. -I would like the image to be public, anyone could see that image in any browser through its link. But, my second doubt: the content of the avatar field in my database is a File, and when I go to that link I see the content of the file in the URL and don't see the image. Finally, my last doubt, in my web service I access the image data through request.post_vars. If I print this data I get something like this: Storage {'upload_field': FieldStorage('upload_field', 'myphoto_1.jpg', '\xff\xd8\ ... ... ...\x04\xd9')} I guess I have to store the image in the database with this: row.update(avatar=request.post_vars['upload_field']) Am I right? Thank you very much! --