Returning an Image from a DataScript

The avi.http.response function returns a simple web page, such as a custom 404 error page. However, making such a page more seamless or branded may require incorporating images, icons, or pictures. DataScripts can serve these images, but it requires a few extra steps.

When a client requests an image, or an HTML page that has an image embedded within it, the DataScript can return the image, however the image must first be converted to Base64 encoding. There are many free sites that will do this. Upload the desired image to a site such as; it will convert the image to a Base64-encoded version. Copy and paste the encoded output into your DataScript. In the HTML below, replace the xxxx with the Base64-encoded image. Also change the image type to the appropriate, such as GIF, JPEG or PNG.

<img src='data:image/gif;base64, xxxx' />

The images can be embedded within the HTML or as standalone requests. DataScripts can be any size, so using larger images is not an issue.


--  This simple page returns an embedded GIF within the HTML
page = "<html><body><p><center><b>Here is a sample embedded image served from a DataScript</b></p>"
page = page .. "</center></body></html>"
avi.http.response(200, {content_type="text/html"}, page)