开发者

get remote image into string

开发者 https://www.devze.com 2023-02-28 22:22 出处:网络
The question says it all: how to get a remotely hosted image into a string. I will later use XMLHTTPPost to upload the content. This is javascript question, for those who don\'t read tag line.

The question says it all: how to get a remotely hosted image into a string. I will later use XMLHTTPPost to upload the content. This is javascript question, for those who don't read tag line.

@Madmartigan: the script itself is executed in rather odd manner: user uses javascript: to append the script from the remove host. (this gives access to the user cookie session, which we need in order to proceed) This generates form, giving user ability to setup some texts. (this is easy bit) When user clicks upload the script must get an image hosted on remote host. I am trying to get the image from the remote host as a string and then use something like the function below to convert it to binary. So, how do I do that?

function toBin(str){
 var st,i,j,d;
 var arr = [];
 var len = str.length;
 for (i = 1; i<=len; i++){
                //reverse so its like a stack
  d = str.charCodeAt(len-i);
  for (j = 0; j < 开发者_StackOverflow中文版8; j++) {
   st = d%2 == '0' ? "class='zero'" : "" 
   arr.push(d%2);
   d = Math.floor(d/2);
  }
 }
        //reverse all bits again.
 return arr.reverse().join("");
}

I should mention, that I managed to find things like:

var reader = new FileReader();
reader.onload = function() {
  previewImage.src = reader.result;
}
reader.readAsDataURL(myFile);

However, they are very browser dependent and therefore not very useful.

I am trying to avoid using base64 because of the redundant size increase.


EDIT: take a look here. Take should help you: http://www.nihilogic.dk/labs/exif/ or maybe here: http://jsfromhell.com/classes/binary-parser the only way to store binary data into string in javascript context is to use base64/base128 encoding. But I never tried it myself to do that in case of a image. There are many JavaScript base encoder/decoder out. Hope this helps you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号