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);
5
  • possible duplicate of How to get a DOM Element from a JQuery Selector Commented Aug 19, 2015 at 8:56
  • What do you mean by converting to object? Are you trying to get binary data of the image? Commented Aug 19, 2015 at 8: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 Commented Aug 19, 2015 at 9:00
  • @MotiAzu Check the edit Commented Aug 19, 2015 at 9:04
  • This should answer your question stackoverflow.com/questions/20903812/… Commented Aug 19, 2015 at 9:06

1 Answer 1

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);
};
Sign up to request clarification or add additional context in comments.

5 Comments

howa can i convert var img = $('<img id="dynamic">'); to file ?
@Omar check the second one.
in object.filename or .data ?
The Problem is when is not changed this block isn't executed so i post null with Ajax
may be you want to take a look at document.createElement() method.

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.