当前位置: 代码迷 >> Web前端 >> 本map片上传即时预览
  详细解决方案

本map片上传即时预览

热度:218   发布时间:2013-10-30 12:56:22.0
本地图片上传即时预览

// 图片预览功能
var preivew = function(file, container){
  // 允许上传的图片格式
  var allowExt = ['jpg', 'gif', 'bmp', 'png', 'jpeg'];
  var Picture = function(file, container){
    var height = 0, widht = 0, ext = '', size = 0, name = '', path = ''; var self = this;
    var tempPath = '';
    if(file){
      name = file.value;
      if (window.navigator.userAgent.indexOf("MSIE")>=1){
        file.select();
        tempPath = document.selection.createRange().text;
        if(tempPath!=""){
          path = tempPath;
        }

      }else if(file.files){
        try{
          path = file.files.item(0).getAsDataURL();
        }catch(e){
         ? ?if(window.URL){
          path = URL.createObjectURL(file.files[0]);
      ??   }
       ? ? ?}
     ? ? ??}else{
       ? ? ?path = file.value;
     ? ? ??}
  ? ? ? }else{
?      throw "bad file";
    }
    ext = name.substr(name.lastIndexOf("."), name.length);
    if(container.tagName.toLowerCase() != 'img'){
      throw "container is not a valid img label";
      container.visibility = 'hidden';
    }


    height = container.height;
    widht = container.widht;
    size = container.fileSize;
    this.get = function(name){
      return self[name];
    }

    this.isValid = function(){
      if(allowExt.indexOf(self.ext) !== -1){
        throw 'the ext is not allowed to upload';
        return false;
      ??}
      return true;
    }
    if(!this.isValid()){
      alert('the ext is not allowed to upload');
      return false;
    ??}
    container.src = path;
    container.alt = name;
  };

  try{
    var pic = new Picture(file, container);
  }catch(e){
    alert(e);
  }
};

  相关解决方案