【发布时间】:2018-11-02 02:45:59
【问题描述】:
我有这段代码在上传之前显示图像预览。但是我正在使用 Angular 5,所以我有一个 .ts 文件而不是 .js 文件。我怎样才能在 Angular 5 中做同样的事情?我还想在所有浏览器中显示图像。
我的 HTML:
<input type='file' onchange="readURL(this);"/>
<img id="blah" src="http://placehold.it/180" alt="your image"/>
我的 CSS:
img {
max-width:180px;
}
input[type=file] {
padding: 10px;
background: #2d2d2d;
}
我的 JavaScript:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById('blah').src=e.target.result
};
reader.readAsDataURL(input.files[0]);
}
}
【问题讨论】:
-
只需删除
function关键字..应该是它.. -
我收到诸如“HTMLElement”类型上不存在属性“src”和“EventTarget”类型上不存在属性“结果”等错误,尽管我的代码相同
标签: javascript angular image file-upload path