gpt4 book ai didi

javascript - HTML5 : readAsDataURL

转载 作者:行者123 更新时间:2023-11-28 20:30:46 26 4
gpt4 key购买 nike

如果用户将图像拖到浏览器中,我已在代码中添加了一个 eventListerner 进行注册。然后我想获取图像的src。我在本地使用 xampp 工作。这是行不通的。控制台显示“匿名函数”和 Uncaught Error 。

    document.body.addEventListener('drop', function (e) {
e.stopPropagation();
e.preventDefault();
file = e.dataTransfer.files[0];

reader.readAsDataURL( file );
console.log("dropped");
console.log("file: "+reader.readAsDataURL( file ).result);
});

最佳答案

这里是一个示例,您可以引用它来帮助您解决问题,它显示了拖放方法以及文件选择器方法。我无法更具体地说明您的代码,因为您只提供了一个代码片段,没有提供控制台中显示的完整错误,并且没有 jsfiddle 来演示您的问题。

CSS

#progress_bar {
margin: 10px 0;
padding: 3px;
border: 1px solid #000;
font-size: 14px;
clear: both;
opacity: 0;
-moz-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
}
#progress_bar.loading {
opacity: 1.0;
}
#progress_bar .percent {
background-color: #99ccff;
height: auto;
width: 0;
}
#display {
width: 500px;
height: 150px;
}
div#drop_div {
width:400px;
height:300px;
background:#ccc;
border:10px dotted #aaa;
text-align:center;
font:22px Georgia;
color:#666;
line-height:300px;
}

HTML

<input type="file" id="files" name="file" />
<button id="cancel">Cancel read</button>
<ul id="list"></ul>
<div id="drop_div">Drag and drop a file here</div>
<div id="progress_bar">
<div class="percent">0%</div>
</div>
<div>Base64 encoded result</div>
<textarea id="display"></textarea>

Javascript

/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/*global global */

(function (global) {
"use strict";

var reader,
progress,
display,
iFiles,
bCancel,
dropZone,
filebox,
list;

function abortRead() {
if (reader) {
reader.abort();
}
}

function errorHandler(evt) {
switch (evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert("File Not Found!");
break;
case evt.target.error.NOT_READABLE_ERR:
alert("File is not readable");
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
alert("An error occurred reading this file.");
};
}

function updateProgress(evt) {
// evt is an ProgressEvent.
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + "%";
progress.textContent = percentLoaded + "%";
}
}
}

function read(file) {
var output = [];

output.push('<li><strong>', escape(file.name), '</strong> (', file.type || 'n/a', ') - ',
file.size, ' bytes, last modified: ',
file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a',
'</li>');

list.innerHTML = '<ul>' + output.join('') + '</ul>';

// Reset progress indicator on new file selection.
progress.style.width = "0%";
progress.textContent = "0%";

reader = new FileReader();
reader.onerror = errorHandler;
reader.onprogress = updateProgress;
reader.onabort = function (e) {
alert('File read cancelled');
};

reader.onloadstart = function (e) {
document.getElementById('progress_bar').className = 'loading';
};

reader.onload = function (e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = "100%";
progress.textContent = "100%";
setTimeout("document.getElementById('progress_bar').className='';", 2000);
display.value = e.target.result;
}

// Read in the image file as a binary string.
reader.readAsDataURL(file);
}

function handleFileSelect(evt) {
read(evt.target.files[0]);
}

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

read(evt.dataTransfer.files[0]);
}

function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}

global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
progress = document.querySelector(".percent");
display = document.getElementById("display");
iFiles = document.getElementById("files");
bCancel = document.getElementById("cancel");
dropZone = document.getElementById("drop_div");
filebox = document.getElementById("filebox");
list = document.getElementById("list");

dropZone.addEventListener("dragover", handleDragOver, false);
dropZone.addEventListener("drop", handleFileDropSelect, false);
iFiles.addEventListener("change", handleFileSelect, false);
bCancel.addEventListener("click", abortRead, false);
}, false);
}(window));

关于jsfiddle

关于javascript - HTML5 : readAsDataURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com