<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function showPreview(source) {
var file = source.files[0];
if(window.FileReader) {
var fr = new FileReader();
fr.onloadend = function(e) {
document.getElementById("portrait").src = e.target.result;
};
fr.readAsDataURL(file);
}
}
</script>
</head>
<body>
<br />
<input type="file" name="upload" onchange="showPreview(this)" multiple="multiple">
<img id="portrait" src="" width="70" height="75">
<input type="submit" onclick="showImage()" value="上传"/>
</form>
</body>
</html>
我想实现点击 上传后,能在tmp目录下看到上传的图片
你js代码把文件以base64编码形式展示了出来,是为了让用户上传文件之前能够预览对吧。
文件的IO操作需要用后端来实现,如果你只是做web前端开发的话,就没有必要研究这个东西,如果你是后端开发者的话可以尝试一下,相关的资料很多,我写个示例吧,后端用php为例:
html实现:
<!DOCTYPE html>php实现(file.php):
<?php