JS 函数返回 null - 帮助

发布于 2024-11-19 02:51:42 字数 1280 浏览 1 评论 0原文

我正在尝试修改示例 http://www.html5rocks .com/en/tutorials/file/dndfiles/#toc-reading-files 使函数handleFileSelect(evt) 返回 reader.result;我的意思是让函数返回图像的 base64 等。 我尝试用函数编写它,但它仅返回 null :( 所以我的问题是如何让函数返回base64?

至于现在我尝试写这个片段...

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', theFile.name, '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
    return reader.result;
  }

所有有用的评论表示赞赏:)

I am trying to modify the example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files to make function handleFileSelect(evt) return reader.result; I mean to make function return base64 for image or so.
I tried to write it with function but it returns null only :(
So my question is how to make the function return base64?

As for now I tried to write this snippet...

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                            '" title="', theFile.name, '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
    return reader.result;
  }

All useful comments are appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-11-26 02:51:42

我不详细了解 FileReader 对象,但看起来它从 URL 异步读取数据。这意味着当您的函数返回 reader.result 时,FileReader 对象尚未完成文件读取。在调用 onload 回调(或发生其他错误情况)之前,该过程不会完成。

因此,您的函数在读取仍在异步发生时返回。因此,结果尚未确定。结果将在 onload 回调旁边或(我猜)在其他表示错误条件(onabort、onerror 等)的回调内可用。

I don't know the FileReader object in detail, but it looks like it reads data from a URL asynchronously. That means that when your function returns reader.result, the FileReader object isn't done reading the file yet. That doesn't finish until the onload callback is called (or some other error condition occurs).

So, your function returns while the reading is still happening asynchronously. Thus the result has not yet been determined. The result would be available on side the onload callback or (I'm guessing), inside other callbacks that would signify error conditions (onabort, onerror, etc..).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文