0

I am developing hybrid mobile application where I'm uploading images to object storage, but as of now I'm able to upload single as well as multiple images but, max size up to 2MB each. I just want to compress that image before uploading .

I need some help regarding that please.

var file_name;
var j;
var variablearray = [];
var filenamearray;

$(document).ready(function() {

  if (window.File && window.FileList && window.FileReader) {
    var items = [
      [],
      []
    ];
    var arrraysize = [];
    $("#myFile").on("change", function(e) {
      if (this.files) {
        var FR = new FileReader();
        // here am selecting multiple image using below loop
        for (i = 0; i < this.files.length; i++) {
          var inp = document.getElementById('myFile');
          filenamearray = [];
          // to get file selected file names
          /*for(j=0;j<inp.files.length;j++)
          {*/
          file_name = inp.files.item(i).name;
          variablearray[i] = inp.files.item(i).name;
          // filenamearray.push(variablearray[i]);

          // console.log("yos :",variablearray[j]);
          // below onload function am using for image preview                  
          FR.onload = function(e) {

            $("<span class=\"pip\">" +
              "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + variablearray[i] + "\"/>" +
              "<br/><span class=\"remove\">Remove image</span>" +
              "</span>").insertAfter("#myFile");
            $(".remove").click(function() {
              $(this).parent(".pip").remove();
            });

            ready[i] = e.target.result;
            // console.log("ready value at index 0 :",ready[0]);
            console.log("ready length ", ready.length - 1);
            arrraysize.push(e.target.result);
            // console.log("my array is:",arrraysize.length);
          }
          FR.readAsDataURL(this.files[i]);
          // }
        }
      }

      $(document).ready(function() {
        $("form").submit(function() {

          console.log("in submit button:", arrraysize.length);
          //console.log("in submit button2:",filenamearray.length);

          /*filenamearray.push("image1");
          filenamearray.push("image2");
          filenamearray.push("image3");*/
          for (var k = 0; k < arrraysize.length; k++) {
            console.log("Arraysize inside loop :", arrraysize.length);

            var date = new Date();
            var time = date.toJSON();

            var form = new FormData($("#upload_image")[k]);
            //var abc = "file="+arrraysize[k]+"&Name="+filenamearray[k];
            var abc = "file=" + arrraysize[k] + "&Name=S_no_" + serial_no + "_" + time + "_" + k;

            // console.log(" file name with position :",filenamearray[k]);
            //below script am using to upload
            //                    
            $.ajax({
              type: "POST",
              url: 'http://CordovaApp.jsp',
              data: abc,
              crossDomain: true,
              success: function(result) {
                console.log("in success ", result);
                alert("success");
                window.location.reload();
              },
              error: function(er) {
                alert(er);
              }
            });
            // i just want to compress image before uploading
          }
        });
      });
    });
  }
  else {
    alert("Your browser doesn't support to File API");
  }
});
Omar
  • 32,302
  • 9
  • 69
  • 112
Abhinay
  • 17
  • 5
  • Code please what you have done so far –  Aug 03 '17 at 05:32
  • above is html code – Abhinay Aug 03 '17 at 05:45
  • image resizing is ok or what for this ? – Jok3r Aug 03 '17 at 05:49
  • @jok3r I just want to compress the image dynamically before uploading ,like if user will select more than 2 mb size it should automatically compress that file upto 2 mb and then uplaod to server – Abhinay Aug 03 '17 at 05:57
  • still not clear... please kindly specify what kind of limit: do you have an upload max limit or an image size max limit on the server or...??? I would be useful if you post which error do you receive. – deblocker Aug 03 '17 at 06:08
  • @deblocker we are not giving uploading limit from backend as well as front end for image but more than 2mb size of image not able to upload. – Abhinay Aug 03 '17 at 06:13
  • @deblocker- what am trying is - am selecting more than 2mb size of image from html page (id =myfile) and getting that myfile in var inp at js page.and sending that inp to url(server) ,but upto 2 mb is going but more than 2 mb not able to send – Abhinay Aug 03 '17 at 06:16
  • so i just want to compress that imp (which contain image) upto 2 mb and then wants to upload – Abhinay Aug 03 '17 at 06:19
  • Possible duplicate of [Compress images on client side before uploading](https://stackoverflow.com/questions/5607396/compress-images-on-client-side-before-uploading) – deblocker Aug 03 '17 at 06:48

0 Answers0