How to insert/pass image to view using js

64 views
Skip to first unread message

Maurice Waka

unread,
Oct 19, 2019, 5:23:17 AM10/19/19
to web2py-users
Hello
I'm trying to pass an image to view from db or static files using js.
I' trying to create an iteration that can pass a text or image or video to the view, so far being able to pass only text to view.
The code below :
        <script>
            $
(function() {
               
var getMessageText, message_side, sendMessage;
                message_side
= 'left';
                getMessageText
= function() {
                   
var $message_input;
                    $message_input
= $('.message_input');
                   
return $message_input.val()
               
};
                sendMessage
= function(text) {
                   
var $messages, message;
                   
if (text.trim() === '') {
                       
return
                   
}
                    $
('.message_input').val('');
                    $messages
= $('.messages');
                    message_side
= message_side === 'left' ? 'right' : 'left';
                    message
= new Message({
                        text
: text,
                        message_side
: message_side
                   
});
                    message
.draw();
                   
return $messages.animate({
                        scrollTop
: $messages.prop('scrollHeight')
                   
}, 300)
               
};
                $
('.send_message').click(function(e) {
                   
return sendMessage(getMessageText())
               
});
                $
('.message_input').keyup(function(e) {
                   
if (e.which === 13) {
                       
return sendMessage(getMessageText())
                   
}
               
});
                sendMessage
(`{{=XML(name3,sanitize=True)}}`);
                setTimeout
(function() {
                   
return sendMessage(`url: "{{=URL('static','images/testimage.jpg')}}"<br>{{=XML(test_text,sanitize=True)}}`)
               
}, 2000)
           
})
       
}.call(this));
   
</script>


With this code instead of getting the image file, it returns a path to the file.
What I need is the image or video instead.
How can I solve this?
Regards

Maurice Waka

unread,
Oct 19, 2019, 8:42:36 AM10/19/19
to web2py-users
This seems to work....
return sendMessage(`<img src="{{=URL('static','images/testimage.jpg')}}" max-height="200px"/><br>{{=XML(test_text,sanitize=True)}}`)

--
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/a0m-Fmz1VWg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/a4baa446-1fe4-4732-b641-965a17c111b3%40googlegroups.com.

Maurice Waka

unread,
Oct 19, 2019, 10:43:39 AM10/19/19
to web2py-users
Hello
I need to pass the image link from the controller to js
controller function():
    imgs = ["<img src=\"{{=URL('static','images/3.jpg')}}\" style=\"max-height='100px';border-radius:7px;\"/>"]

view page
code.....
return sendMessage(`{{=imgs}}<br>{{=XML(test_text,sanitize=True)}}`)
so far i'm getting only a string as previously stated. If I insert the string directly, then i'm able to view the image but not importing from the controller.
Regards

Ruslan Gareev

unread,
Oct 22, 2019, 4:23:46 AM10/22/19
to web2py-users
try:
controller function():
   imgs = ['<img src="' + URL('static','images/3.jpg') + '" style="max-height=100px;border-radius:7px;"/>']




суббота, 19 октября 2019 г., 19:43:39 UTC+5 пользователь Maurice Waka написал:
To unsubscribe from this group and all its topics, send an email to web...@googlegroups.com.

Dave S

unread,
Oct 22, 2019, 5:37:51 PM10/22/19
to web2py-users


On Tuesday, October 22, 2019 at 1:23:46 AM UTC-7, Ruslan Gareev wrote:
try:
controller function():
   imgs = ['<img src="' + URL('static','images/3.jpg') + '" style="max-height=100px;border-radius:7px;"/>']




<IMG> tags work great for static files.  And for files in the db that use the upload field type, the "app/download?obfusctatedfilename" provides a handy way to download the file.  (The usual way of finding obfuscatedfilename is to do a select() based on the original filename (which upload fields can also store), or a tag, or the id if you already have that in hand.)  You can also use that url in an <IMG> tag.

In your view,
<IMG src={{=URL("download", "obuscatedfilename")}} />
or
{{=IMG(_src=URL("download", "obuscatedfilename"))}}

If the file in the DB isn't in an upload field, you can use response.stream(filepath, request) or set the content type and return the read results, like Massimo's example in
<URL:https://groups.google.com/d/msg/web2py/iP3vSfKn-8c/UzINycmsBwAJ>.
My previous example,
<URL:https://groups.google.com/d/msg/web2py/MTJoZc1Kh6I/oYjxBn2KCQAJ>
used cStringIO, but it seems that can be skipped especially if it is an image file.

/dps


Ruslan Gareev

unread,
Oct 23, 2019, 3:25:08 AM10/23/19
to web2py-users
Hi Maurice! Improving Dave's answer, you can make in your controller function:

imgs = [IMG(_src=URL('path', 'to/your/image'), _style="your style"))



среда, 23 октября 2019 г., 2:37:51 UTC+5 пользователь Dave S написал:

Maurice Waka

unread,
Oct 23, 2019, 8:01:04 AM10/23/19
to web2py-users
Thanks for the response.
Regards

--
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/a0m-Fmz1VWg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/fe4c9c25-8dfd-4393-9b92-f2c2b8281ed3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages