显示选择在Chrome和mozilla中上传的图像 [英] display image selected for uploading in Chrome and mozilla

查看:73
本文介绍了显示选择在Chrome和mozilla中上传的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个Web应用程序中,必须使用javascript显示选择上传的图像,然后再将其上传到服务器.

In one of my web application I have to show the image which selected for uploading before it uploaded to the server , using javascript .

我有这段代码...在Mozilla中,它运行得很好并且很好.但不是在Safari或Chrome浏览器中..请帮助

I had this code ... It is working pretty well and fine in Mozilla . But not in Safari or Chrome .. Please help

// Handle file while select a new file
$('#file').change(function(){
        $('#img_size').val((this.files[0].size)/1000000);
                   handleFiles(this.files);
});


// handle files

function handleFiles(files) {





   for (var i = 0; i < files.length; i++) {
    var file = files[i];

    var imageType = /image.*/;

    if (!file.type.match(imageType)) {
        continue;
    }


     var img=document.getElementById('fake_img');
     img.src = file;
     img.onload = function() {



    };



    var reader = new FileReader();
    reader.onload = (function(aImg) {
        return function(e) {
            aImg.src = e.target.result;
        };
    })(img);
    reader.readAsDataURL(file);
}

}

推荐答案

此代码在Chrome浏览器中正常工作

This code is working fine in chrome

http://jsfiddle.net/PrNWY/13/

它以chrome& FF.

Its shows the selected image in chrome & FF.

更新

您可以使用以下代码检查文件读取器的功能

you can check file reader capability with following code

// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
    // Great success! All the File APIs are supported.
    alert('The File APIs are fully supported in this browser.');
 } else {
    alert('The File APIs are not fully supported in this browser.');
 }

在我的野生动物园中,它失败了,因为它不完全支持文件API.

In my safari it failed because of it is not supporting file API fully.

这篇关于显示选择在Chrome和mozilla中上传的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆