【发布时间】:2019-10-21 12:58:27
【问题描述】:
我正在尝试将我的页面内容从默认的 .png 文件动态更改为用户为预览而加载的任何内容。我设法实现了类似于Preview an image before it is uploaded 的东西。我接受了这个建议并将我的 div 包裹在 <form runat="server"></form>
我的 HTML 元素如下所示:
<form runat="server">
<label class="control-label col-md-4 col-lg-3" for="photo" id="FotoLbl">
Foto anfügen
<div class="inline-help form-label" data-help="&bdquo;Ein
" data-heading="Foto einfügen" data-help-key="offers.photo"><i onClick="myFunction('fotoModal','commentOK6')" class="fa fa-question-circle-o" id="FotoHilfe"></i></div>
</label>
<div class="col-md-8 col-lg-9">
<div class="row fileinput-control">
<img id="preview" src="Anzeige%20erstellen-Dateien/default_offers_photo-edd8e5ff2d549a9fa1a898b23119931ebd0e745.png" width="500px" height="360px" style="padding-left:15px;" onload="addDeleteBttn()"/>
<br/>
<input type="file" id="image" style="display: none;" />
<!--<input type="hidden" style="display: none" value="0" name="remove"remove">-->
<a href="javascript:changeProfile()">
<div class="file-btns">
<span class="btn btn-default btn-file" id="bildBttn">
<i title="Bild auswählen" class="fa fileinput-new fa-file-image-o"></i></span></div>
</a>
<a id="removeBttnFrame" href="javascript:removeImage()">
</a>
<div class="col-sm-6">
<textarea class="form-control copyright" placeholder="Geben Sie hier die Bildquelle an: Fotograf, Lizenz, ...
Ohne Quellenangaben kann das Bild nicht angezeigt werden.
" name="offer[photo_copyright]" id="offer_photo_copyright"></textarea>
<div class="fileinput-description"></div>
</div>
</div>
</div>
</form>
</div>
在下面的 .js 代码中,您可以看到我通常如何上传图像预览,效果非常好。但是,当我在 FLASK 上提供页面时,图像预览不会加载。
function changeProfile() {
$('#image').click();
}
$('#image').change(function() {
var imgPath = this.value;
var ext = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
if (ext == "gif" || ext == "png" || ext == "jpg" || ext == "jpeg")
readURL(this);
else
alert("Please select image file (jpg, jpeg, png).")
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(input.files[0]);
reader.onload = function(e) {
$('#preview').attr('src', e.target.result);
// $("#remove").val(0);
};
}
}
function removeImage() {
$('#preview').attr('src', 'noimage.jpg');
// $("#remove").val(1);
$('#preview').attr('src', 'Anzeige%20erstellen-Dateien/default_offers_photo-edd8e5ff2d549a9fa1a898b23119931ebd0e745.png');
}
function addDeleteBttn() {
var removeBttn = document.createElement("BUTTON");
removeBttn.title = "Entfernen";
removeBttn.innerHTML = '<i class="fa fa-trash-o"></i>'
removeBttn.className = "removeBttnClass";
document.getElementById("removeBttnFrame").appendChild(removeBttn);
}
我需要能够在烧瓶上提供 HTML 页面并让用户加载图像预览。由于它只是一个图像预览,没有在烧瓶上实际上传文件,我不明白为什么烧瓶在显示浏览器上发生的图像预览时出现问题。我对 FLASK/HTML/javascript 很陌生,但仍然不了解他们的大部分概念。这里发生了什么?我错过了什么?
【问题讨论】:
标签: javascript html flask