2

Iam doing a project in html, javascript only. I have a function that convert image to base64, see the code below.

function getBase64Image()
{
    p = document.getElementById("picField").value;
    img.setAttribute('src', p);
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataURL = canvas.toDataURL("image/png");
    var r = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
    base64 = r;
    alert(base64);
}

but the problem is when I deployed my website, means when I placed my html file on iis, its not working, means in local file system it is showing complete base 64 format like iVb...., but on iis it giving base64 code as just "base,". so why it is not working on iis i dont know, so please help me and send me html file that will work on iis too.

5
  • This is client side code and shouldn't have anything to do with IIS. You probably have a Same Origin Policy problem. What kind of a field is document.getElementById("picField") and what does the URL look like that is in it? Commented Mar 5, 2012 at 9:28
  • ofcourse, same html file showing base64 string clearly , but when placed on iis , its not showing, why? Commented Mar 5, 2012 at 9:31
  • <input type="file" id="picField" onchange="f()" > Commented Mar 5, 2012 at 9:32
  • That will not work I'm afraid. I'm surprised it works locally. What browser are you using? Commented Mar 5, 2012 at 9:33
  • possible duplicate of base64 javascript not running on iis, in some browsers too, why? Commented Aug 9, 2012 at 19:38

1 Answer 1

1

I'm fairly sure you have a Same Origin Policy problem.

When you run your page on IIS, it runs in a different context than locally: Instead of a file:// URL, it runs on a http:// one.

What you are trying to do is fetch a local image file and load it into a canvas on a remote site. That is not possible using traditional JavaScript for security reasons. (I'm not sure how this works even locally - in my understanding, it shouldn't. But anyway.)

You will need to use the HTML 5 file API which allows JavaScript direct access to local files. I'll look whether I can dig up some related SO questions.

Update: this should help:

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

4 Comments

HOW CAN WE RESOLVE OUR ISSUE? PLEASE HELP ME
@Raja see the link in the answer. You'll need to use a slightly different concept here.
iam sorry, i unable to do, and its for mozilla, not working on IE
@Raja yeah, it won't work in IE yet. Maybe in IE10... You'd have to serve one version in IE, and the other in Firefox/Chrome. I don't have any hands-on experience with this so I can't help any further, I don't have the time to try this out myself right now

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.