[英]Control image upload size in textarea of HTML Form
我正在使用下面的WIZWIG编辑器“ http://js.nicedit.com/nicEdit-latest.js ”发布到下面的HTML表单中。 我想控制上传图像的大小。 图片大小应为200px高,300px宽。 我尝试了CSS,HTML和JavaScript都无济于事。 无法解决这个问题...
<style>
.nicEdit-main{
background-color: white;
}
</style>
<script type="text/javascript" src="http://js.nicedit.com/nicEdit- latest.js"></script>
<script type="text/javascript">
bkLib.onDomLoaded(function() { nicEditors.allTextAreas({buttonList : ['bold','italic','underline','strikeThrough','html','forecolor','link','upload','unlink','removeformat']}) });
</script>
<form name="comments" action="<?php echo $_SERVER['PHP_SELF']; ?>"method="post" onsubmit="return checkForm();">
<?php if ($isGuest) { ?>
Comment:<br><br>
<div style="background: #fff;">
<textarea style="width: 100%;" name="comment" cols="111px" rows="4">disabled for guest account</textarea>
</div>
<input type="submit" name="action" disabled value="Add Comment" />
<?php } else { ?>
Comment:<br><br>
<div style="background: #fff;">
<textarea style="width: 100%;" name="comment" cols="111px" rows="4"></textarea>
</div>
<input type="submit" name="action" value="Add Comment" />
<?php //header("Location: test.php");
} ?>
</form>
尽管几年前这是不可能的(您必须将文件发送到服务器端才能处理文件信息),但是现在可以使用FileReader()对象和纯JS实现。
您使用.files[0]
来获取文件,然后在DOM上创建一个临时img对象(以便执行此过程),然后(在FileReader()
的帮助下FileReader()
将文件分配给该DOM对象。您只需比较文件的宽度或高度即可。 这是一个示例如何执行此操作:
imgfile.onchange = function(e) { var oFile = document.getElementById("imgfile").files[0]; //Get the selected file by the user var img = document.getElementById("imgdom"); //get the reserve image element on the dom to be able do all our processing with this object type document.getElementById("imgdom").style.display = "none";//This is optional if you want to show or not the image to the user var reader = new FileReader(); //This is the magic object that allows you to process a file on the client side reader.onload = function (e) { console.log(e.total); // file size img.src = e.target.result; // putting file in dom without server upload. alert(img.width); //Do what ever condition you want here //if img.width > 200 do this, else do that }; reader.readAsDataURL(oFile ); //return false; };
<form method="post" enctype="multipart/form-data" onsubmit="return false;"> <input type="file" name="imgfile" id="imgfile"> </form> <div> <img id="imgdom" src="" />
以下CSS代码已解决了此问题:
.nicEdit-main img {
width: 350px;
}
img[src*="http://somedomain.com"] {
width: 350px;
}
感谢所有发表评论的人...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.