HTML5拖放/文件API缩略图上传器无法正常工作

时间:2011-09-08 00:11:19

标签: javascript html5 drag-and-drop fileapi

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

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

任何帮助?!

感谢。 丹尼

(以下是我的代码)

       <!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>

1 个答案:

答案 0 :(得分:0)

您可以使用window.URL.createObjectURL / window.URL.revokeObjectURL获取文件对象的uri。它优于readAsDataURL

var img = document.createElement('img');
img.src = (window.URL || window.webkitURL || window.mozURL).createObjectURL(files[0]);

Firefox和Google Chrome支持该功能。