javascript - FileReader read as DataUrl - Stack Overflow

I am workingon this app on the Chrome App Store. Evidently the File API changed, so I needed to imple

I am working on this app on the Chrome App Store. Evidently the File API changed, so I needed to implement FileReader to get the local URL of a file that is dropped onto the page.

function drop(evt) {

    v = evt.target.id;

    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.

    var f = files[0];

    var reader = new FileReader();

          // Closure to capture the file information.
          reader.onload = (function(theFile) {
            return function(e) {
              document.getElementById(v).src = e.target.result;
            };
          })(f);

   reader.readAsDataURL(f);
}

What I am trying to do is load the URL of a song that is dropped onto the page into an HTML5 Audio tag's src attribute. I can't figure out what I am doing wrong with this drop function.

Does anybody have any ideas?

I am working on this app on the Chrome App Store. Evidently the File API changed, so I needed to implement FileReader to get the local URL of a file that is dropped onto the page.

function drop(evt) {

    v = evt.target.id;

    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.

    var f = files[0];

    var reader = new FileReader();

          // Closure to capture the file information.
          reader.onload = (function(theFile) {
            return function(e) {
              document.getElementById(v).src = e.target.result;
            };
          })(f);

   reader.readAsDataURL(f);
}

What I am trying to do is load the URL of a song that is dropped onto the page into an HTML5 Audio tag's src attribute. I can't figure out what I am doing wrong with this drop function.

Does anybody have any ideas?

Share Improve this question asked May 31, 2011 at 20:45 Isaac LewisIsaac Lewis 1,1992 gold badges15 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

The solution in the end was simple. You cannot test the File Reader API from a local file system. This creates an Security Error - caused by the empty file headers from the local file system. To fix it, simply test it on any web server.

Hmm, this looks right to me. What is the current behavior? You're attaching the drop event to the audio element itself? You can get rid of the closure, since you're only using the first file and not looping through the FileList

Here's a similar DnD file snippet (it uses images instead of an src): http://www.html5rocks./tutorials/file/dndfiles/#toc-selecting-files-dnd

As an alternative, you could use a blobURL:

window.URL = window.URL || window.webkitURL;

function drop(evt) {
  ...
  var file = evt.dataTransfer.files[0];
  ...
  audio.src = window.URL.createObjectURL(file);
  audio.onload = function(e) {
    window.URL.revokeObjectURL(this.arc); // clean up
  };
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265068a4619404.html

相关推荐

  • javascript - FileReader read as DataUrl - Stack Overflow

    I am workingon this app on the Chrome App Store. Evidently the File API changed, so I needed to imple

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信