上一篇
想象一下:你是某电商网站的开发者,用户吐槽“上传商品图总失败,要么格式不对,要么直接报错!”😤 老板拍桌要求“必须支持批量上传,还要能预览!”💼 这时候,一篇保姆级ASP图片上传攻略就是你的救命稻草!本文结合2025年最新实践,用口语化讲解+代码示例,让你30分钟搞定这个刚需功能!
<!-- 上传表单 --> <form enctype="multipart/form-data" method="post" action="upload.asp"> <input type="file" name="files[]" multiple accept="image/*"> <button type="submit">🚀上传图片</button> </form> <!-- 预览区域 -->div id="preview"></div> <script> // 前端实时预览 document.querySelector('input').addEventListener('change', function(e){ const files = e.target.files; const preview = document.getElementById('preview'); preview.innerHTML = ''; Array.from(files).forEach(file=>{ const reader = new FileReader(); reader.onload = (e)=>{ const img = document.createElement('img'); img.src = e.target.result; img.style.maxWidth = '150px'; preview.appendChild(img); } reader.readAsDataURL(file); }); }); </script>
<%@ Language=VBScript %> <!--#include file="UploadClass.asp" --> <% ' 1. 创建上传对象 Set objUpload = New Upload_5xSoft objUpload.Path = Server.MapPath("uploads/") ' 存储路径 objUpload.MaxSize = 5 * 1024 * 1024 ' 5MB限制 objUpload.AllowExtensions = "jpg,jpeg,png,gif" ' 白名单 ' 2. 安全验证(防注入+防超限) If objUpload.Error Then Select Case objUpload.ErrorCode Case 1: Response.Write "❌文件太大!" Case 2: Response.Write "❌格式不支持!" Case Else: Response.Write "❌未知错误!" End Select Response.End End If ' 3. 保存文件(自动重命名防覆盖) For i = 1 To objUpload.FileCount Dim NewFileName NewFileName = Year(Now) & Month(Now) & Day(Now) & "_" & Hour(Now) & Minute(Now) & Second(Now) & "." & objUpload.FileExt(i) objUpload.SaveAs objUpload.Path & "\" & NewFileName, i Response.Write "✅" & objUpload.FileName(i) & " 上传成功!路径:" & NewFileName & "<br>" Next %>
危险操作:用户上传shell.asp;jpg
(伪造后缀)
防御代码:
' 严格检查MIME类型 Dim AllowedMIME AllowedMIME = Array("image/jpeg", "image/png", "image/gif") If Not InArray(objUpload.FileMIME(i), AllowedMIME) Then Response.Write "❌非法文件类型!" Response.End End If
危险操作:上传../../web.config
试图覆盖配置文件
防御代码:
' 禁止特殊字符 Dim BadChars BadChars = Array("../", ".", ":", "*", "?", "<", ">", "|") For Each char In BadChars If InStr(1, objUpload.FileName(i), char, vbTextCompare) > 0 Then Response.Write "❌文件名含非法字符!" Response.End End If Next
防御配置(在web.config
添加):
<system.web> <httpRuntime maxRequestLength="102400" executionTimeout="3600"/> </system.web>
在watermark.asp
修改透明度参数:
' 调用GD库添加水印 Set objGD = Server.CreateObject("Persits.GD") objGD.WatermarkImage "uploads/" & NewFileName, "logo.png", 0.3 ' 透明度30%
在CSS添加媒体查询:
@media (max-width: 768px) { #preview img { max-width: 100% !important; } }
CREATE TABLE UploadLogs ( ID INT IDENTITY(1,1) PRIMARY KEY, FileName NVARCHAR(255), FilePath NVARCHAR(512), UploadTime DATETIME DEFAULT GETDATE(), UserID INT -- 关联用户表 );
掌握ASP图片上传的安全三板斧(类型验证+大小限制+路径安全),再搭配本文的防覆盖重命名和实时预览技巧,绝对能让你在开发中少踩90%的坑!现在就去服务器部署测试吧,遇到问题欢迎评论区交流~ 👇👇👇
本文由 业务大全 于2025-08-23发表在【云服务器提供商】,文中图片由(业务大全)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://xdh.7tqx.com/wenda/702187.html
发表评论