Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to preview local images before uploading them via a form?

Tags:

To be more specific, I want to use a form with one or more file input fields used for images. When those fields are changed, I'd like to show a preview of the associated image, before sending the data to the server.

I've tried a number of javascript approaches, but I always run into security errors. I wouldn't mind using java or flash, as long as the solution degraded gracefully for those users who didn't have them. (They wouldn't get previews, and they wouldn't get an annoying 'install this thing' either.)

Has anyone done this in a simple, reusable way?

P.S. I know there's a sandbox, but does the sandbox have to be in a dark, locked room with all the windows blacked out?

like image 375
Matt Van Horn Avatar asked May 28 '09 17:05

Matt Van Horn


People also ask

How do I display an image before uploading HTML?

The most efficient way would be to use URL. createObjectURL() on the File from your <input>. Pass this URL to img. src to tell the browser to load the provided image.

How do I preview photos?

Right-click on an image file and you should now see an Image Preview command in the popup menu. Click that command to view the image in Windows Photo Viewer (Figure D). Photo Viewer instantly pops up. Click the magnifying glass icon and move the slider to zoom in or out of the image.

How do you preview an image before it is uploaded using jquery?

First, we have a division element that contains the “img” tag. Using Jquery, we will change the “src” attribute of the “img” tag on upload to preview the image. The second element is the “input” tag. It is essential to specify type = “file” in this context.


2 Answers

No need for fancy stuff. All you need is the createObjectURL function, which creates a URL that can be used as the image src, and can come straight from a local file.

Let's say you selected a couple of images from the user's computer using a file input element (<input type="file" />). Here's how you would create previews for image files for it:

function createObjectURL(object) {     return (window.URL) ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object); }  function revokeObjectURL(url) {     return (window.URL) ? window.URL.revokeObjectURL(url) : window.webkitURL.revokeObjectURL(url); }  function myUploadOnChangeFunction() {     if(this.files.length) {         for(var i in this.files) {             var src = createObjectURL(this.files[i]);             var image = new Image();             image.src = src;             // Do whatever you want with your image, it's just like any other image             // but it displays directly from the user machine, not the server!         }     } } 
like image 183
dev Avatar answered Sep 23 '22 06:09

dev


The first step is finding out the image path. JavaScript is allowed to interrogate the upload control for a filename/path, but (for reasons of security) various browsers show different things to the JS engine than they display to the user - they tend to keep the filename intact so you can at least validate its extension, but you may get c:\fake_path\ or some similarly obfuscated thing prepended to the filename. Trying this on various browsers will give you an idea as to what gets returned as a real path, and what gets faked out, and where.

The second step is displaying the image. It's possible to display local images if you know their paths, via img tags with file:// source URLs, if the user's browser allows the file:// scheme. (Firefox doesn't, by default.) So if you can get the user to tell you what the full path to the image is, you can at least try to load it.

like image 21
Dan Davies Brackett Avatar answered Sep 20 '22 06:09

Dan Davies Brackett