简体   繁体   English

将动态插入的图像的文件路径推送到javascript中的数组中

[英]Push filepath of dynamically inserted image into array in javascript

I'm trying to make an image slider. 我正在尝试制作图像滑块。 I have an array of objects like this: 我有一个这样的对象数组:

var slides = [
    {
        img: 'images/one.jpg',
        text: 'First Image' 
    },
    {
        img: 'images/two.jpg',
        text: 'Second Image'
    }
];

I need to have the functionality to add more images into the image slider. 我需要具有将更多图像添加到图像滑块的功能。 Hence I need to obtain the file path of the image I will be uploading and push that file path into this slides array. 因此,我需要获取将要上传的图像的文件路径,并将该文件路径推送到此slides数组中。

I have gone through this SO answer, which effectively teaches how to upload and display an image, but my needs are different. 我已经通过了这个 SO答案,它有效地教了如何上传和显示图像,但是我的需求有所不同。

HTML HTML

        <div id="slide-container">

        </div>

        <div id="panel-container">
            <form>
                <label>
                    <span>Image Text</span>
                    <input type="text" class="form-control" id="image-related-text">
                </label>
                <label>
                    <span>Duration</span>
                    <input type="text" class="form-control" id="time-duration">
                </label>
                <div id="d">
                    <input type="file" id="image-upload">
                </div>

                <button id="add" disabled="true">Add to Container</button>
            </form>
        </div>

JS JS

var global_image_url = "";

// add the image and the relevant text in the slider on click of this button
$('#add').click(function(){

    var imageRelatedText = $('#image-related-text').val();
    var timeDuration = $('#time-duration').val();

    // a new object to be pushed inside the 'slides' array
    var temp_obj = {
        img: 'nothing for now :|', //this should contain the image url
        text: imageRelatedText
    };
})

$('#image-upload').change(function(){
    global_image_url = readUrl(this);
    console.log(global_image_url);  
});

// this function gets called when an image file is chosen
function readUrl(input){

    var img = $('<img>');
    var local_image_url = ""; // thought I would store the file path in this variable

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {

            local_image_url = JSON.stringify(e.target.result);

            img.attr('src', e.target.result);
            img.attr('height','100%');
            img.attr('width','97%');
        }
        reader.readAsDataURL(input.files[0]);
    }

    console.log(local_image_url); // chrome developer tools freezes
    return local_image_url;
}

I thought e.target.result would give the url but that's not the case. 我以为e.target.result会提供网址,但事实并非如此。 It's just another object (printed in the console once). 它只是另一个对象(在控制台中打印一次)。

So how do I achieve my requirements? 那么我如何达到我的要求?

Thanks! 谢谢!

yes e.target.result will the url, but remember FileReader reader the file content as asynchronous, if you want to output the value, you need to add some callback like : 是的e.target.result将是URL,但请记住FileReader reader将文件内容视为异步,如果要输出该值,则需要添加一些回调,例如:

$('#image-upload').change(function(){
   readUrl(this,function(){
         console.log(global_image_url);  
    });

});

// this function gets called when an image file is chosen
function readUrl(input,callback){

    var img = $('<img>');
    var local_image_url = ""; // thought I would store the file path in this variable

    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            typeof callback == 'function' && callback(e.target.result);
            local_image_url = JSON.stringify(e.target.result);

            img.attr('src', e.target.result);
            img.attr('height','100%');
            img.attr('width','97%');
        }
        reader.readAsDataURL(input.files[0]);
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM