HTML5 File API始终返回[object file]的DataURI。我究竟做错了什么? [英] HTML5 File API always returns DataURI of [object file]. What am I doing wrong?

查看:108
本文介绍了HTML5 File API始终返回[object file]的DataURI。我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我一直试图制作一个使用拖放和文件API的缩略图加载器。

Today I have been attempting to make a thumbnail up loader which uses drag and drop and the file API.

我觉得好像我已经相当远了。但是作为我的图像文件中的源提供的DataURI总是作为[目标文件]返回,因此不显示图像。

I feel as if i have got reasonably far. But the DataURI that is provided to be the source in my image file ALWAYS returns as [object file] and therefore doesn't show the image.

任何帮助?!

谢谢。
Danny

Thanks. Danny

(以下是我的代码)

       <!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
    #thumbnailPreview 
    {
        width:149px;
        height:137px;
        border:3px dashed #333;
        border-radius:10px; 
        text-align:center;
    }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
    $("document").ready(function() {
        if (!!FileReader) 
        {
            $("#thumbnailPreview").append("<p>Drag image here to set as thumbnail</p>")
            $("#thumbnailUploadBox").hide();

            var thumbnailPreview = document.getElementById("thumbnailPreview")
            // init event handlers
            thumbnailPreview.addEventListener("dragenter", preventDefault, false);
            thumbnailPreview.addEventListener("dragexit", preventDefault, false);
            thumbnailPreview.addEventListener("dragover", preventDefault, false);
            thumbnailPreview.addEventListener("drop", drop, false)

            function preventDefault(evt)
            {
                evt.stopPropagation();
                evt.preventDefault();   
            }

            function drop(evt)
            {
                evt.stopPropagation();
                evt.preventDefault();
                var files = evt.dataTransfer.files;
                var noOfFiles = files.length;
                if (noOfFiles === 1) 
                {
                    handleImages(files);    
                }
                else 
                {
                    alert("You appear to be attempting to upload more or less than 1 image. You can only have one thumbnail image. Please try again."); 
                }
            }

            function handleImages(files) 
            {
                var file = files[0];
                {
                    if(file.type.indexOf("image") == 0)
                    {
                        $("#thumbnailPreview").empty().append('<p>Working on it!</p><progress id="progressbar"></progress>');   
                        var reader = new FileReader();
                        reader.onloadend = handleReaderLoadEnd;
                        reader.onprogress = handleReaderProgress;
                        reader.readAsDataURL(file);
                    }
                    else 
                    {
                        alert("The file you dragged wasn't an image. Please drag another file before attempting to upload");    
                    }
                }
            }
            function handleReaderLoadEnd(e) {
              $("#thumbnailPreview").empty().prepend('<img id="thumbnailPreviewImage">');
              $("#thumbnailPreviewImage").attr({src: e.target.result});
            }
            function handleReaderProgress(evt)
            {
                if (evt.lengthComputable) 
                {   
                    var loaded = (evt.loaded / evt.total);
                    $("#progressbar").attr({ value: loaded * 100 });
                }
            }
        } 
    });
</script>
</head>

<body>
<form action="lighthouseMaker.php" enctype="application/x-www-form-urlencoded" method="post">   
    <div id="thumbnailPreview">
        <p>Upload an Image 149 x 137px</p>
    </div>
    <div id="thumbnailUploadBox">
        <input type="file" name="thumbnail">
    </div>
</form>
</body>
</html>


推荐答案

您正在设置FileReader的 onloadend 处理程序返回值 handleReaderLoadEnd()而不是设置回调并传递ecent。相反,你想要:

You're setting the FileReader's onloadend handler to the return value of handleReaderLoadEnd() rather than setting the callback and it passing the ecent. Instead, you want:

reader.onloadend = handleReaderLoadEnd;

....

function handleReaderLoadEnd(e) {
  $("#thumbnailPreview").empty().prepend('<img id="thumbnailPreviewImage">');
  $("#thumbnailPreviewImage").attr({src: e.target.result});
}

这篇关于HTML5 File API始终返回[object file]的DataURI。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆