SoFunction
Updated on 2025-04-03

JavaScript image preview and upload (IE compatible)

This article has shared the specific code for preview and uploading of js images for your reference. The specific content is as follows

var dailiApply = {

   change: function (evt) {
    ();
    var pic = ("preview"),
     file = ("f");

    var ext=((".")+1).toLowerCase();
    // gif cannot be displayed in IE browser for the time being    if(ext!='png'&&ext!='jpg'&&ext!='jpeg'){
     alert("The image format must be in png or jpg or jpeg format!");
     return;
    }
    var isIE = (/MSIE/)!= null,
     isIE6 = (/MSIE 6.0/)!= null;

    if(isIE) {
     ();
     var reallocalpath = ().text;

     // IE6 browser sets the src of img to the local path to directly display pictures     if (isIE6) {
       = reallocalpath;
     }else {
      // Due to security issues, IE is not able to display local pictures directly by setting img's src, but it can be achieved through filters       = "progid:(sizingMethod='image',src=\"" + reallocalpath + "\")";
      // Set the src of img to a transparent picture encoded by base64. Cancel display the default picture of the browser       = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
     }
    }else {
     var file_arr = ;
     var ul = $(".weui_uploader_files");
     if(file_arr.length < 7) {
      for(var key in file_arr) {
       if(file_arr.hasOwnProperty(key)) {
        var f = file_arr[key];
        var url = (f);
        var reader = new FileReader();
        (f);
        (f);
        n +=1;
        if(n < 7) {
         reader._onload = function(e) {

          // Stitch together the html that displays the preview image          var list = $("<li class='weui_uploader_file' style='position: relative'>" +
           "<img id='preview" + n + "' class=preview_li' style='width: 100%;height: 100%'>" +
           "<span id='delImg" + n+ "' style='position: absolute; top: 0; right: 4px; color: #e4007f'>X</span></li>");
          (list);
          // Put the converted image address in img          var pic = ('preview' + n);
          // = ;
          =url;
          (f, .7,undefined);
          //(f);
          ('delImg' + n).addEventListener("click", function () {
           $(this).parent().remove();
           --n;
          });

         };
         reader._onload();
        }else {
         $.alert("Upload up to 6 pictures");
         n = 6;
        }
       }
      }
     }else {
      $.alert("Upload up to 6 pictures");
     }
    }
    return false;
   },
   /**
     * @param {Object} f input selected image Required
     * @param {String} quality The quality of image compression [0, 1]
     * @param {String} output_img_type The type of output image
     */
   compress: function (f, quality, output_img_type) {
    var mime_type = "image/jpeg";
    if(output_img_type!=undefined && output_img_type=="image/png"){
     mime_type = "image/png";
    }
    createImageBitmap(f).then(function(imageBitmap) {
     var max = 1000; // Set maximum resolution     var c_w = '';
     var c_h = '';
     var width = ;
     var height = ;
     // Scale scale     if (width > max || height > max) {
      if (width > height) {
       c_w = max;
       c_h = max * height / width;
      } else {
       c_h = max;
       c_w = max * width / height;
      }
     }else {  // No zoom      c_w = width;
      c_h = height;
     }

     var canvas = ('canvas');
      = c_w;
      = c_h;
     var ctx = ('2d');
     (imageBitmap,0,0, width, height, 0, 0, c_w, c_h);
     (function(blob){
      (blob);
     },mime_type, quality);
    });
   },
   submit: function () {
    var content = $(".weui_textarea").val().trim();
    var xhr = new XMLHttpRequest();
    var fd = new FormData(('uploadForm'));
    $.each(images,function(i,e){
     ("images", e);
    });
    ("remark", content);
    ("substationproxyId", 8);
    (images);
    (fd);
    if(content) {
     $.ajax({
      url: ,
      type: "POST",
      data: fd,

      processData: false, // tell jQuery not to process the data
      contentType: false, // tell jQuery not to set contentType
      beforeSend: function (xhr) {
       $.showLoading();
       $(this).prop("disabled", true)
      },
      success: function (json) {
       (json);
       $.hideLoading();
       $(this).prop("disabled", false);
       if( == 0) {
        $.alert("Save successfully", function () {
         ();
        })
       }else if( == "-101") {
        $.alert('Error:' +, function () {
          = ;
        });
       }else {
        $.alert(, function () {

        })
       }
      }
     });
    }else {
     $.alert('Please enter content');
    }

   }

  };

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.