图片上传中,文件的校验

图片上传中,文件的校验
      来源:奥索网
  
  图片上传中,文件的校验
作者:tonglw

这是我的一点心得,希望能对大家有所帮助。有不足之处请大家包涵,多多指教。


客户端校验

<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>选择图片</title>
<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>
<!--
Sub button1_onclick
if trim(window.form1.file1.value)="" then
alert "选择图片!"
exit sub
end if

sfile=trim(window.form1.file1.value)
sfile=ucase(right(sfile,4))
if sfile=".JPG" or sfile=".GIF" then
window.form1.submit
else
alert "选择 *.JPG 或 *.GIF 文件!"
exit sub
end if
End Sub

Sub file1_onfocus
sfile=window.form1.file1.value
sfile=ucase(right(sfile,4))
if sfile=".JPG" or sfile=".GIF" then
window.form1.photo.style.visibility="visible"
window.form1.photo.src =window.form1.file1.value
end if
End Sub
-->
</SCRIPT>
</head>
<body style="font-size: 9pt">
<p align="center">选择图片</p>
<FORM action="add.php" method=POST method=post enctype="multipart/form-data" id=form1 name=form1>
<div align="center">
<center>
<table border="1" cellspacing="0" width="354" style="font-size: 9pt" bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr>
<td width="350" colspan="2" align="center"><img id=photo border="0" src="" style="visibility:hidden" height="50"></td>
</tr>
<tr>
<td width="83" align="right">选择图片:</td>
<td width="267"><INPUT type="file" id=file1 name=file1></td>
</tr>
<tr>
<td width="350" colspan="2" align=center><br>
<INPUT type="button" value="提交" id=button1 name=button1></td>
</tr>
</table>
</center>
</div>
</FORM>
</body>
</html>

------------------------------------------------------------------

服务器端校验
add。php 校验部分

<?php

if(!(($file1_type=="image/gif")||($file1_type=="image/pjpeg"))) {
echo "<div align=center ><a href='javascript:window.history.back(-1) ; ' >请选择GIf或JPEG的文件 ,后退</a></div>";
exit ;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload 是 Element UI 文件组件,它可以进行文件校验。要对上文件进行校验,你可以使用 Element UI 提供的属性和事件。 首先,在 el-upload 组件上设置 accept 属性来指定允许上文件类型。例如,如果你只想允许上图片文件,可以设置 accept="image/*"。 接下来,你可以使用 before-upload 属性来自定义文件校验的逻辑。before-upload 属性接收一个函数,该函数会在每个文件前被调用。你可以在该函数进行校验,并返回一个 Boolean 值来指示是否继续上文件。 以下是一个示例代码,展示了如何对文件进行校验: ```html <template> <el-upload action="/upload" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" accept="image/*" > <el-button slot="trigger">点击上</el-button> <div slot="tip">只能上图片文件</div> </el-upload> </template> <script> export default { methods: { beforeUpload(file) { // 自定义校验逻辑 const isValidType = file.type === 'image/jpeg' || file.type === 'image/png'; const isValidSize = file.size / 1024 / 1024 < 2; // 文件大小限制为2MB if (!isValidType) { this.$message.error('只能上JPEG和PNG格式的图片'); } if (!isValidSize) { this.$message.error('文件大小不能超过2MB'); } return isValidType && isValidSize; }, handleSuccess(response, file) { // 文件成功的处理逻辑 this.$message.success('文件成功'); }, handleError(error, file) { // 文件失败的处理逻辑 console.error(error); this.$message.error('文件失败'); }, }, }; </script> ``` 在上述示例,beforeUpload 方法对文件进行了类型和大小的校验,如果校验不通过,则阻止文件并给出相应的错误提示。handleSuccess 和 handleError 方法分别用于处理文件成功和失败后的逻辑。 注意,上述示例的消息提示部分使用的是 Element UI 的 Message 组件,你可以根据自己的需求来替换为其他方式的提示。 希望以上信息能够对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值