7

Hi I have a page where I show user's profile image that is loaded like this:

$("#imgProfile").attr('src', 'http://myserver/user.png')

Now I need to submit this image using HTML 5 File API, but to do this I need to first convert my image to File API. All the samples that I saw at internet work together with input type="file", but I already have that image, I'm not choosing the image from local computer.

All the examples like this one https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications use File API from input type="file" element

1
  • 1
    The preview that is referring to is most likely a preview BEFORE uploading it. If it's on your server you just load it like a normal image. Creating img tags in either html or through java script. Commented May 20, 2015 at 21:54

2 Answers 2

3

The file is already on your server. You can use it, as Yair Nevet's answer says.

If you really want to make it a File object, maybe you can use this(I don't know if it works).

var getFilelikeBlobFromImageElement = (function closure() {
        var canvasElement = document.createElement("canvas");

        return function(imageElement) {
                canvasElement.width = imageElement.width;
                canvasElement.height = imageElement.height;
                var canvasContext = canvasElement.getContext("2d");
                canvasContext.drawImage(imageElement, 0, 0);

                var imageData = canvasContext.getImageData(0, 0, imageElement.width, imageElement.height).data;
                var buffer = new Uint8Array(imageData.length);
                for(var index = 0; index < imageData.length; index++)
                    buffer[index] = imageData[index];

                var imageBlob = new Blob(buffer);
                imageBlob.lastModifiedDate = new Date();
                imageBlob.name = /\/([^\/]+)$/.exec(imageElement.src)[1];

                return imageBlob; // A `File`-like `Blob` object.
            };
    })();

Usage:

getFilelikeBlobFromImageElement(document.getElementById("imgProfile"));

To get the Blob from a <canvas>

To make a Blob File-like

Sign up to request clarification or add additional context in comments.

Comments

0

Don't be confused with the File API, if the image already exists in the server, all left from you to do is to declare an <img> tag with a reference to the image URL and to display it:

<img src="serverAddress\yourImage.png" alt="Image Something" />

1 Comment

But it doesn't seem like previewing is what he wants. He already have the file uploaded on the server and just wish to display it. So I think he has confused the example with what he wants to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.