6

我想在上传之前显示图像的预览。我找到了适用于 ie6 和 firefox 的部分解决方案,但尚未在 ie7 或 ie8 中对其进行测试。但我想要一个适用于 safari、ie7 和 ie8 的解决方案。下面是结合ie6和firefox方案得到的方案:

function preview(what) {
if(jQuery.browser.msie) {
document.getElementById("preview-photo").src=what.value;
return;
}
else if(jQuery.browser.safari) {
document.getElementById("preview-photo").src=what.value;
return;
}
document.getElementById("preview-photo").src=what.files[0].getAsDataURL();
//  alert(jQuery("#preview-photo").height());
//  alert(jQuery("#preview-photo").width());
var h = jQuery("#preview-photo").height();  
var w = jQuery("#preview-photo").width();//assuming width is 68, and height is floating
if ((h > 68) || (w > 68)){
if (h > w){
jQuery("#preview-photo").css("height", "68px");
jQuery("#preview-photo").css("width", "auto");
}else {
jQuery("#preview-photo").css("width", "68px");
jQuery("#preview-photo").css("height", "auto");
}
}
}

getAsDataURL() 部分在 firefox 中有效,“src=what.value”部分在 ie6 中有效,但在 safari 中有效,“src=what.value”在 ie7 和 ie8 中也有效吗?如果没有,是否有一些解决方案也适用于那里?如果我能让图像预览在 5 或 6 个浏览器中工作,我会很高兴。如果不是,那么唯一的选择是拥有两个表单和另一个表单的图像上传部分?

4

5 回答 5

5

你可以使用打击功能。在 IE7+ 和 Firefox 和 chrome 上测试

function loadname(img, previewName){  

var isIE = (navigator.appName=="Microsoft Internet Explorer");  
var path = img.value;  
var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();  

 if(ext == "gif" || ext == "jpeg" || ext == "jpg" ||  ext == "png" )  
 {       
    if(isIE) {  
       $('#'+ previewName).attr('src', path);  
    }else{  
       if (img.files[0]) 
        {  
            var reader = new FileReader();  
            reader.onload = function (e) {  
                $('#'+ previewName).attr('src', e.target.result);  
            }
            reader.readAsDataURL(img.files[0]);  
        }  
    }  

 }else{  
  incorrect file type  
 }   
}  

<input type="file" name="image" onchange("loadname(this,'previewimg')") >
<img src="about:blank" name="previewimg" id="previewimg" alt="">
于 2012-04-11T16:26:38.997 回答
2

在 Firefox 和 chrome 中工作

<input type="file" id="file" />
<div id="div"></div>
<script type="text/javascript">
function view() {
    var f = document.getElementById("file").files[0];
    var reader = new FileReader();
    reader.onload = (function(evt) {  //evt get all value
        document.getElementById('div').innerHTML = 
            "PIC :<img src=" +
            evt.target.result + "/>" ;
    });
    reader.readAsDataURL(f);
}
</script>
于 2012-10-12T17:12:10.383 回答
1

如果这样做,这将是一个严重的安全问题。您无法在用户计算机中预览文件。您必须将文件上传到服务器,并在文件成功上传后显示文件的预览。

于 2009-12-11T11:43:36.123 回答
1

链接到blob,就像链接到任何图像一样,当然,一旦给出或更改了即将上传的图像,您必须立即更新src,这是我的做法,我没有时间测试它在 Windows 浏览器(即 IE)中。

这个例子也实现了基本的验证:http: //jsfiddle.net/J3GP7/

    <style>
        #image_preview {
            display:none;
        }
    </style>

    <form>
        <p>
            <label for="image">Image:</label><br />
            <input type="file" name="image" id="image" />
        </p>
    </form>
    <div id="image_preview">
        <img src="#" /><br />
        <a href="#">Remove</a>
    </div>

    <script>
    /** 
    onchange event handler for the file input field.
    * It emplements very basic validation using the file extension.
    * If the filename passes validation it will show the image 
    using it's blob URL and will hide the input field and show a delete
    button to allow the user to remove the image
    */
    jQuery('#image').on('change', function () {
        ext = jQuery(this).val().split('.').pop().toLowerCase();
        if (jQuery.inArray(ext, ['gif', 'png', 'jpg', 'jpeg']) == -1) {
            resetFormElement(jQuery(this));
            window.alert('Not an image!');
        } else {
            file = jQuery('#image').prop("files")[0];
            blobURL = window.URL.createObjectURL(file);
            jQuery('#image_preview img').attr('src', blobURL);
            jQuery('#image_preview').slideDown();
            jQuery(this).slideUp();
        }
    });

    /**
    onclick event handler for the delete button.
    It removes the image, clears and unhides the file input field.
    */
    jQuery('#image_preview a').bind('click', function () {
        resetFormElement(jQuery('#image'));
        jQuery('#image').slideDown();
        jQuery(this).parent().slideUp();
        return false;
    });

    /** 
     * Reset form element
     * 
     * @param e jQuery object
     */
    function resetFormElement(e) {
        e.wrap('<form>').closest('form').get(0).reset();
        e.unwrap();
    }
    </script>
于 2013-05-10T17:19:37.303 回答
0

jquery ajax文件上传

$('[name="send"]').click(function(){

   view();

   v_data = {
                news_header : $('[name="news_header"]').val(),
                news_auth : $('[name="news_auth"]').val(),
                news_image : image, //this var taking for view() function what i use before
                news_news : $('[name="news_news"]').val()    

            };

   $("#show").html(v_data.news_Header + " " + v_data.news_auth + " "+ v_data.news_image + " "+ v_data.news_news );

   $.ajax({
        type    :   "POST",
        url     :   './insert_news_data.php',
        enctype: 'multipart/form-data',        
        data    :   v_data,

        success: function(data) {
            alert(data);
        }
   });


});
于 2012-11-27T04:22:59.847 回答