0

我编写了代码来获取图像文件并生成上传图像的预览。当我读取图像文件数据并生成预览时,它不会显示图像预览,但是当我在绑定图像源之前发出警报时,它可以正常工作。

我应该如何解决这个问题?

下面是代码片段

function changeSrc(file) {

            var reader = new FileReader();

            // array with acceptable file types
            var accept = ["image/png", "image/jpeg", "image/jpg", "image/gif"];

            // if we accept the first selected file type
            if (accept.indexOf(file.files[0].type)> -1) {
                if (file.files && file.files[0]) {
                    reader.readAsDataURL(file.files[0]);

                   If i remove this alert then preview creates problem
                   --> ***alert(reader.result);***

                    $(#previewField).attr(src, reader.result);

                }
            }
    }
4

0 回答 0