使用ajax发送默认图像

时间:2015-08-19 08:54:42

标签: javascript jquery ajax

大家好,我怎么能在这段代码中将图像转换为对象,例如:

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;

这样做的原因是,如果输入文件未设置,我是否自动上传此图像:

<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"/>

修改

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);

    });
}

如果没有输入文件,我如何设置我的默认图像以使用ajax发送?

EDIT2

如果event.target.files;为null,我如何从新图像设置文件:

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);

1 个答案:

答案 0 :(得分:0)

我想你可以这样做:

object.filename = file.name || defaultImg;

其中defaultImg是一个包含带src的默认img的变量。

像:

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);
};