将url转file类型

function getImageFileFromUrl(url, imageName,callback) {
  // imageName一定要带上后缀
    var blob = null;
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", url);
    xhr.setRequestHeader('Accept', 'image/jpeg');
    xhr.responseType = "blob";
    xhr.onload = () => {
	console.log('111')
	if (xhr.status == 200) {
      blob = xhr.response;
      let imgFile = new File([blob], imageName, {type: 'image/jpeg'});
	  console.log(imgFile)
      callback.call(this,imgFile);
    }};
    xhr.send();
}



  $(function () {
	getImageFileFromUrl('https://XXX 图片url路径','fileName.png',function(file){
		//生成file文件
		console.log(file)
		//file文件进行处理
		var r = new FileReader()
        r.readAsDataURL(file)
        r.onload = function (e) {
          //生成base64
          var base64 = e.target.result;
          $("img").attr('src', base64);         
        }
	});
  })

异步获取其值,还是要向大佬学习~

你可能感兴趣的:(前端)