javascript - 从 JavaScript 打开图像文件(不适用于手机)

标签 javascript jquery filereader

我正在尝试使用 JavaScript 打开图像文件。它在 Chrome 上成功运行(没有显示错误),但图像拒绝在我的 iPhone 上显示。这是因为图像太大(例如 DATA url 太长)吗?

我的代码使用FileReader,获取img.src,并将其输出为img标签,如下所示:

if(window.FileReader) {   //do this
  $('input').change(function() {
    	$("#result").html("SELECTING IMAGE ... ... ...");
      var fr = new FileReader;

      fr.onload = function() {
          var img = new Image;

          img.onload = function() { 
              //I loaded the image and have complete control over all attributes, like width and src, which is the purpose of filereader.
              $.ajax({url: img.src, async: true, success: function(result){
              		$("#result").html("<img src='ajax-loader.gif' />");
              		setTimeout(function(){
                		$("#result").html("<img src='" + img.src + "' /> <br/> <br/>" + "<a href='" + img.src + "'>View in browser</a>");
                    console.log("Finished reading Image");document.getElementById('iPUT').style.opacity=0.01;
              		}, 1000);
          		}});
          		
          		
          };
          
          img.src = fr.result;
      };
      
      fr.readAsDataURL(this.files[0]);
      
  });

} else {
  alert("You don't support FileReader");
}
<html><head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <title>iViewr</title>
    <!-- The style.css file allows you to change the look of your web pages.
         If you include the next line in all your web pages, they will all share the same look.
         This makes it easier to make new pages for your site. -->
    <link href="/style.css" rel="stylesheet" type="text/css" media="all">
  </head>
  <body cz-shortcut-listen="true">
  <div onclick="document.getElementById('iPUT').style.opacity=0.01">
    <button onclick="document.getElementById('result').style.zoom=0.5;this.blur();">Zoom out</button>
    <button onclick="document.getElementById('result').style.zoom=1.0;this.blur();">Zoom normal</button>
    <button onclick="document.getElementById('result').style.zoom=1.5;this.blur();">Zoom in</button>
    <br>
    </div>
    <div id="inputDIV" onclick="document.getElementById('iPUT').style.opacity=0.5"><input type="file" id="iPUT" accept="image/*" capture="camera" style="opacity: 0.5;"></div>
    <div id="result">Please choose a file to view it.</div>
    <script src="index.js"></script>

  
</body></html>

您可以在此处查看它的实际情况:http://iviewr.neocities.org/

提前致谢!

最佳答案

对输入选择的文件使用 URL.createObjectURL(),如下所示。 感谢@dandavis。

$('input').change(function(){
  var resultPreview = document.getElementById('result');
  var file = document.querySelector('input[type=file]').files[0]; //selects the very first file
  
  var fr = new FileReader();
  
  fr.addEventListener('load', function(){
    //once the FileReader is loaded,
    resultPreview.src = fr.result;
    console.log("Finished reading Image");document.getElementById('iPUT').style.opacity=0.01;
    document.getElementById('help').innerHTML="You can choose another image by tapping the screen again.";
  }, false);
  
  if (file){ //if there even is a first file
    fr.readAsDataURL(file); //don't want to mess with Data URLs!! this calls the function above
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <title>iViewr</title>
    <!-- The style.css file allows you to change the look of your web pages.
         If you include the next line in all your web pages, they will all share the same look.
         This makes it easier to make new pages for your site. -->
    <link href="/style.css" rel="stylesheet" type="text/css" media="all">
  </head>
  <body>
  <div onclick="document.getElementById('iPUT').style.opacity=0.01">
    <button onclick="document.getElementById('result').style.zoom=0.5;this.blur();">Zoom out</button>
    <button onclick="document.getElementById('result').style.zoom=1.0;this.blur();">Zoom normal</button>
    <button onclick="document.getElementById('result').style.zoom=1.5;this.blur();">Zoom in</button>
    <br />
    </div>
    <div id='help'>Tap the middle of the screen to select an image for the computer to guess.</div>
    <div id='inputDIV' onclick="document.getElementById('iPUT').style.opacity=0.5"><input type="file" id='iPUT' accept="image/*" capture="camera"></div>
    <img id='result' src=''/>
    <script src='index.js'></script>

  </body>
</html>

关于javascript - 从 JavaScript 打开图像文件(不适用于手机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845568/

相关文章:

javascript - Qualtrics-使用 javascript 创建数组然后执行计算

jquery - 使用 jQuery 从选择中获取下一个或上一个元素

javascript - 避免重复的 onclick 语句

java - 我如何将 Long 变量与 null 进行比较

javascript - JSZip 在解压缩时更改文件内容 (pdf)

javascript - 如何使用指向相同 URL 的 React-Router <Link> 重新渲染组件

Fancybox 的 Javascript/Jquery 回调 是 否 确认

Javascript 文本操作

jquery - Hello Bar-like 代码

javascript - 将文本区域的内容保存为带有 .csv 扩展名的纯 txt 文件