0

Hi guys how can i convert a image to object in this code for example:

var img = $('<img id="dynamic">'); 
img.attr('src', 'http://macreationdentreprise.fr/wp-content/uploads/2012/06/ouvrir-un-restaurant.jpeg');
var obj=img.ConvertToObject();
// so i can now use obj.filename; and obj.data;

The reason to do that is if i wan't to upload this image automatically if the input file isn't set:

<input id="picture_zone_upload_input_ID" type="file" name="picture_zone_upload_input" class="picture_zone_upload_input" multiple title="Choose a file to upload"/>

EDIT

function configure_zone_upload() {
    $("#picture_zone_upload_input_ID").change(function (event) {
        $.each(event.target.files, function (index, file) {
            var reader = new FileReader();

            reader.onload = function (event) {
                object = {};
                object.filename = file.name;
                object.data = event.target.result;
                //alert("index: " + index);
                upload_img_count++;
                configure_upload_img(object.data, upload_img_count);

                files.push(object);

            };
            reader.readAsDataURL(file);
        });

        var files = event.target.files;
        for (var x = 0; x < files.length; x++) {
            data.append(x, files[x]);
        }
        //alert(files.length);
         AJAX_upload_Profile(data, upload_img_count);

    });
}

If there isn't a input file how can i set the my default image to send it with ajax ??

EDIT2

If event.target.files; is null how can i set file from new image:

var files = event.target.files;
            for (var x = 0; x < files.length; x++) {
                data.append(x, files[x]);
            }
            //alert(files.length);
             AJAX_upload_Profile(data, upload_img_count);
Dev pro
  • 620
  • 1
  • 7
  • 12
  • possible duplicate of [How to get a DOM Element from a JQuery Selector](http://stackoverflow.com/questions/1677880/how-to-get-a-dom-element-from-a-jquery-selector) – nkorth Aug 19 '15 at 08:56
  • What do you mean by converting to object? Are you trying to get binary data of the image? – Moti Azu Aug 19 '15 at 08:58
  • @MotiAzu If the input file is chosen i send data to controller by ajax in form of object so if i can convert this img to object i cant send it woth ajax – Dev pro Aug 19 '15 at 09:00
  • @MotiAzu Check the edit – Dev pro Aug 19 '15 at 09:04
  • This should answer your question http://stackoverflow.com/questions/20903812/how-to-upload-image-file-from-url-using-filereader-api – Moti Azu Aug 19 '15 at 09:06

1 Answers1

0

I guess you can do this:

object.filename = file.name || defaultImg;

where defaultImg is a variable which contains a default img with src.


somthing like:

reader.onload = function (event) {
    object = {};
    object.defaultImg = $('<img>',{"src" : "defaultImg/path/img.jpeg"}); // <--declare here
    object.filename = file.name 
    object.data = event.target.result || object.defaultImg; //<---use it here;
    //alert("index: " + index);
    upload_img_count++;
    configure_upload_img(object.data, upload_img_count);

    files.push(object);
};
Jai
  • 74,255
  • 12
  • 74
  • 103