Saving binary data as file using JavaScript from a browser

This is possible if the browser supports the download property in anchor elements. var sampleBytes = new Int8Array(4096); var saveByteArray = (function () { var a = document.createElement(“a”); document.body.appendChild(a); a.style = “display: none”; return function (data, name) { var blob = new Blob(data, {type: “octet/stream”}), url = window.URL.createObjectURL(blob); a.href = url; a.download = name; a.click(); … Read more

I need my PHP page to show my BLOB image from mysql database

In your current case, you have two upfront options. The first, and the one I don’t recommend if you have numerous images like this, is to use inline base64 encoding. This is done with: <img src=”data:image/jpeg;base64,<?php echo base64_encode($image); ?>” /> A copy/paste version, using your existing code: echo ‘<dt><strong>Technician Image:</strong></dt><dd>’ . ‘<img src=”data:image/jpeg;base64,’ . base64_encode($row2[‘image’]) … Read more

Getting BLOB data from XHR request

Don’t use BlobBuilder in Chrome (tested in OSX Chrome, Firefox 12, Safari 6, iOS Chrome, iOS Safari): ex1 : http://jsfiddle.net/malraux/xGUsu/ (principle) ex2: http://jsfiddle.net/xGUsu/78/ (working with full example) var xhr = new XMLHttpRequest(); xhr.open(‘GET’, ‘doodle.png’, true); xhr.responseType=”arraybuffer”; // Process the response when the request is ready. xhr.onload = function(e) { if (this.status == 200) { // … Read more

Using JavaScript to display a Blob

You can also get BLOB object directly from XMLHttpRequest. Setting responseType to blob makes the trick. Here is my code: var xhr = new XMLHttpRequest(); xhr.open(“GET”, “http://localhost/image.jpg”); xhr.responseType = “blob”; xhr.onload = response; xhr.send(); And the response function looks like this: function response(e) { var urlCreator = window.URL || window.webkitURL; var imageUrl = urlCreator.createObjectURL(this.response); document.querySelector(“#image”).src … Read more

Insert Blobs in MySql databases with php

Problem $sql = “INSERT INTO ImageStore(ImageId,Image) VALUES(‘$this->image_id’,’file_get_contents($tmp_image)’)”; This creates a string in PHP named $sql. Forget about MySQL for a minute, because you’re not executing any query yet. You’re just building a string. The magic of PHP means that you can write a variable name — say, $this->image_id — inside the double quotes and the … Read more

How can I store and retrieve images from a MySQL database using PHP?

First you create a MySQL table to store images, like for example: create table testblob ( image_id tinyint(3) not null default ‘0’, image_type varchar(25) not null default ”, image blob not null, image_size varchar(25) not null default ”, image_ctgy varchar(25) not null default ”, image_name varchar(50) not null default ” ); Then you can write … Read more

Open links made by createObjectURL in IE11

This demo uses Blob URL which is not supported by IE due to security restrictions. IE has its own API for creating and downloading files, which is called msSaveOrOpenBlob. Here is my cross-browser solution that works on IE, Chrome and Firefox: function createDownloadLink(anchorSelector, str, fileName){ if(window.navigator.msSaveOrOpenBlob) { var fileData = [str]; blobObject = new Blob(fileData); … Read more