如何获取图像网址的大小(mb,kb,gb)

时间:2017-06-18 14:16:53

标签: jquery image url

我在网络上看到这个例子,但它得到宽度和高度,我需要图像的重量。我该怎么办?

function getMeta(url){
$("<img/>",{
    load : function(){ alert(this.width+' '+this.height); },
    src  : url
});
}

1 个答案:

答案 0 :(得分:0)

    String endPoint = @"http://example.com/v1/api/";
    String json = @"....";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
    request.Method = "POST";
    request.KeepAlive = false;
    request.AllowReadStreamBuffering = false;

    /* Pretend this middle part does the Authorization with username and password. */
    /* I have actually authenticated using the above method, and passed a key to the request */

    //This part POST the JSON to the API
    using (StreamWriter writeStream = new StreamWriter(request.GetRequestStream()))
        {
            writeStream.Write(json);
            writeStream.Flush();
            writeStream.Close();  
        }

    //This bottom part opens up a console, but never reads or loads the data
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

HTML文件

window.URL    = window.URL || window.webkitURL;
var elBrowse  = document.getElementById("browse"),
    elPreview = document.getElementById("preview"),
    useBlob   = false && window.URL; // set to `true` to use Blob instead of Data-URL

function readImage (file) {
  var reader = new FileReader();

  reader.addEventListener("load", function () {
    var image  = new Image();

    image.addEventListener("load", function () {
      var imageInfo = file.name +' '+
          image.width +'×'+
          image.height +' '+
          file.type +' '+
          Math.round(file.size/1024) +'KB';

      // Show image and info
      elPreview.appendChild( this );
      elPreview.insertAdjacentHTML("beforeend", imageInfo +'<br>');

      if (useBlob) {
        // Free some memory
        window.URL.revokeObjectURL(image.src);
      }
    });
    image.src = useBlob ? window.URL.createObjectURL(file) : reader.result;
  });

  reader.readAsDataURL(file);  
}


elBrowse.addEventListener("change", function() {

  var files = this.files, errors = "";

  if (!files) {
    errors += "File upload not supported by your browser.";
  }

  if (files && files[0]) {

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

      if ( (/\.(png|jpeg|jpg|gif)$/i).test(file.name) ) {
        readImage( file ); 
      } else {
        errors += file.name +" Unsupported Image extension\n";  
      }

    }
  }

  // Handle errors
  if (errors) {
    alert(errors); 
  }

});

有关详细信息,请参阅以下链接中的类似问题 link