ASP图片上传展示
环境准备
1、 安装IIS :确保你的服务器安装了IIS(Internet Information Services)。
2、 创建Web应用 :在IIS中创建一个新的Web应用程序。
3、 配置数据库 :可以使用SQL Server或其他数据库来存储图片的元数据。
文件结构
/ImageUploadApp/bin/images上传的图片将保存在这里/Scripts/StylesDefault.aspxUpload.aspxWeb.config
Default.aspx
这是主页,用于显示已上传的图片。
<%@ Page Language="VB" %>图片展示 已上传的图片
<%Dim conn As New SqlConnection("your_connection_string")Dim cmd As New SqlCommand("SELECT ImageUrl FROM Images", conn)Dim dr As SqlDataReaderconn.Open()dr = cmd.ExecuteReader()While dr.Read()Response.Write("")End Whiledr.Close()conn.Close()%>
Upload.aspx
这是上传页面,包含一个表单用于选择和上传图片。
<%@ Page Language="VB" %>图片上传 上传图片
Upload.aspx.vb
处理图片上传的逻辑。
Imports System.Data.SqlClientImports System.IOPartial Class UploadInherits System.Web.UI.PageProtected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.ClickIf FileUpload1.HasFile ThenDim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") & fileName)' 将文件名存入数据库Dim conn As New SqlConnection("your_connection_string")Dim cmd As New SqlCommand("INSERT INTO Images (ImageUrl) VALUES (@ImageUrl)", conn)cmd.Parameters.AddWithValue("@ImageUrl", fileName)conn.Open()cmd.ExecuteNonQuery()conn.Close()End IfEnd SubEnd Class
Web.config
配置数据库连接字符串。
数据库表结构(Images)
CREATE TABLE Images (Id INT PRIMARY KEY IDENTITY,ImageUrl NVARCHAR(255) not NULL)
相关问题与解答
问题1:如何防止用户上传恶意文件?
解答 :为了防止用户上传恶意文件,可以在上传之前检查文件类型和大小,只允许特定类型的文件(如jpg、png等),并限制文件的大小,可以通过以下代码实现:
If FileUpload1.PostedFile.ContentType = "image/jpeg" OrElse FileUpload1.PostedFile.ContentType = "image/png" ThenIf FileUpload1.PostedFile.ContentLength <= 500000 Then ' 限制文件大小为500KB' 进行文件保存和数据库插入操作End IfEnd If
问题2:如何优化图片的存储和检索性能?

解答 :为了优化图片的存储和检索性能,可以考虑以下几点:
1、 使用CDN(内容分发网络) :将图片存储在CDN上,可以加快全球用户的访问速度。
2、 图片压缩 :在上传时对图片进行压缩,减少存储空间和传输时间。
3、 数据库索引 :为数据库中的图片URL字段添加索引,加快查询速度。
以上内容就是解答有关“ asp图片上传展示 ”的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。
asp.net图片上传代码 用在网站里面
图片上传的代码其实比较简单,思路也易掌握。
我给出一个完整的实例,你复制过去就可以直接使用。
asp上传图片源码
<%class clsUp 文件上传类------------------------Dim Form,FileDim AllowExt_ 允许上传类型(白名单)Dim NoAllowExt_ 不允许上传类型(黑名单)Private oUpFileStream 上传的数据流Private isErr_ 错误的代码,0或true表示无错Private ErrMessage_ 错误的字符串信息Private isGetData_ 指示是否已执行过GETDATA过程------------------------------------------------------------------类的属性Public Property Get VersionVersion=Version 2004End PropertyPublic Property Get isErr 错误的代码,0或true表示无错isErr=isErr_End PropertyPublic Property Get ErrMessage 错误的字符串信息ErrMessage=ErrMessage_End PropertyPublic Property Get AllowExt 允许上传类型(白名单)AllowExt=AllowExt_End PropertyPublic Property Let AllowExt(Value) 允许上传类型(白名单)AllowExt_=LCase(Value)End PropertyPublic Property Get NoAllowExt 不允许上传类型(黑名单)NoAllowExt=NoAllowExt_End PropertyPublic Property Let NoAllowExt(Value) 不允许上传类型(黑名单)NoAllowExt_=LCase(Value)End Property----------------------------------------------------------------类实现代码初始化类Private Sub Class_InitializeisErr_ = 0NoAllowExt= 黑名单,可以在这里预设不可上传的文件类型,以文件的后缀名来判断,不分大小写,每个每缀名用;号分开,如果黑名单为空,则判断白名单NoAllowExt=LCase(NoAllowExt)AllowExt= 白名单,可以在这里预设可上传的文件类型,以文件的后缀名来判断,不分大小写,每个后缀名用;号分开AllowExt=LCase(AllowExt)isGetData_=falseEnd Sub类结束Private Sub Class_Terminate on error Resume Next清除变量及对像 Form = File = oUpFileStream = NothingEnd Sub分析上传的数据Public Sub GetData (MaxSize)定义变量on error Resume Nextif isGetData_=false then Dim RequestBinDate,sSpace,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfoDim sFormValue,sFileNameDim iFindStart,iFindEndDim iFormStart,iFormEnd,sFormName代码开始If < 1 Then 如果没有数据上传isErr_ = 1ErrMessage_=没有数据上传Exit SubEnd IfIf MaxSize > 0 Then 如果限制大小If > MaxSize ThenisErr_ = 2 如果上传的数据超出限制大小ErrMessage_=上传的数据超出限制大小Exit SubEnd IfEnd IfSet Form = () = 1Set File = () = 1Set tStream = ()Set oUpFileStream = () = = () = 0RequestBinDate = iFormEnd = = ChrB (13) & ChrB (10)取得每个项目之间的分隔符sSpace = MidB (RequestBinDate,1, InStrB (1,RequestBinDate,bCrLf)-1)iStart = LenB(sSpace)iFormStart = iStart+2分解项目DoiInfoEnd = InStrB (iFormStart,RequestBinDate,bCrLf & bCrLf)+ = = = tStream, = = = gb2312sInfo = 取得表单项目名称iFormStart = InStrB (iInfoEnd,RequestBinDate,sSpace)-1iFindStart = InStr (22,sInfo,name=,1)+6iFindEnd = InStr (iFindStart,sInfo,,1)sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)如果是文件If InStr (45,sInfo,filename=,1) > 0 ThenSet oFileInfo = new clsFileInfo取得文件属性iFindStart = InStr (iFindEnd,sInfo,filename=,1)+10iFindEnd = InStr (iFindStart,sInfo,&vbCrLf,1)sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart) = GetFileName(sFileName) = GetFilePath(sFileName) = GetFileExt(sFileName)iFindStart = InStr (iFindEnd,sInfo,Content-Type: ,1)+14iFindEnd = InStr (iFindStart,sInfo,vbCr) = Mid(sinfo,iFindStart,iFindEnd-iFindStart) = = iFormStart -iInfoEnd = sFormName,oFileInfoelse如果是表单项目 = = = iInfoEnd tStream, = = = gb2312sFormValue = (sFormName) ThenForm (sFormName) = Form (sFormName) & , & sFormName,sFormValueEnd IfEnd = iFormStart+iStart+2如果到文件尾了就退出Loop Until (iFormStart+2) >= iFormEnd RequestBinDate = Set tStream = NothingisGetData_=trueend ifEnd Sub保存到文件,自动覆盖已存在的同名文件Public Function SaveToFile(Item,Path)SaveToFile=SaveToFileEx(Item,Path,True)End Function保存到文件,自动设置文件名Public Function AutoSave(Item,Path)AutoSave=SaveToFileEx(Item,Path,false)End Function保存到文件,OVER为真时,自动覆盖已存在的同名文件,否则自动把文件改名保存Private Function SaveToFileEx(Item,Path,Over)On Error Resume NextDim oFileStreamDim tmpPathDim nohack 防黑缓冲isErr=0Set oFileStream = CreateObject () = = = File(Item) oFileStream,File(Item)=split(path,.) 重要修改,防止黑客\0断名伪装!!!tmpPath=nohack(0)&.&nohack(ubound(nohack)) 重要修改,防止黑客\0断名伪装!!!if Over thenif isAllowExt(GetFileExt(tmpPath)) tmpPath,2ElseisErr_=3ErrMessage_=该后缀名的文件不允许上传!End ifElsePath=GetFilePath(Path)if isAllowExt(File(Item)) ()nohack=split(Path&GetNewFileName()&.&File(Item),.) 重要修改,防止黑客\0断名伪装!!!tmpPath=nohack(0)&.&nohack(ubound(nohack)) 重要修改,防止黑客\0断名伪装!!! tmpPathloop Until < PathElseisErr_=3ErrMessage_=该后缀名的文件不允许上传!End ifEnd oFileStream = Nothingif isErr_=3 then SaveToFileEx= else SaveToFileEx=GetFileName(tmpPath)End Function取得文件数据Public Function FileData(Item)isErr_=0if isAllowExt(File(Item)) = File(Item) = (File(Item))ElseisErr_=3ErrMessage_=该后缀名的文件不允许上传!FileData=End ifEnd Function取得文件路径Public function GetFilePath(FullPath)If FullPath <> ThenGetFilePath = Left(FullPath,InStrRev(FullPath, \))ElseGetFilePath = End IfEnd function取得文件名Public Function GetFileName(FullPath)If FullPath <> ThenGetFileName = mid(FullPath,InStrRev(FullPath, \)+1)ElseGetFileName = End IfEnd function取得文件的后缀名Public Function GetFileExt(FullPath)If FullPath <> ThenGetFileExt = LCase(Mid(FullPath,InStrRev(FullPath, .)+1))ElseGetFileExt = End IfEnd function取得一个不重复的序号Public Function GetNewFileName()dim ranNumdim dtNowdtNow=Now()ranNum=int(*rnd)+GetNewFileName=year(dtNow) & right(0 & month(dtNow),2) & right(0 & day(dtNow),2) & right(0 & hour(dtNow),2) & right(0 & minute(dtNow),2) & right(0 & second(dtNow),2) & ranNumEnd FunctionPublic Function isAllowExt(Ext)if NoAllowExt= thenisAllowExt=cbool(InStr(1,;&AllowExt&;,LCase(;&Ext&;)))elseisAllowExt=not CBool(InStr(1,;&NoAllowExt&;,LCase(;&Ext&;)))end ifEnd FunctionEnd Class----------------------------------------------------------------------------------------------------文件属性类Class clsFileInfoDim FormName,FileName,FilePath,FileSize,FileMIME,FileStart,FileExtEnd Class%>
ASP图片上传代码//急急急//
<%set upload=new upload_5xsoftset file=(sf_upfile)if <1 <script language=javascript>alert(您没有选择图片。 ~~~\n\n-----请点击浏览按钮,从弹出的窗口中选择要上传的图片。 \n\n-----然后点击上传按钮就可以把图片上传了...\n\n-----不过要注意图片的格式哦~~~);()</script> ifif <10 or >6100*5000 错误:上传的图片大小超过了限制! <a href=(-1)>退回上一步</a> ifupfilename = split(,\)ufn = split(,.)upfileext = ufn(ubound(ufn))if upfileext<>jpg and upfileext<>jpeg and upfileext<>gif and upfileext<>JPG and upfileext<>JPEG and upfileext<>GIF and upfileext<>png and upfileext<> 错误:上传的文件格式不对! <a href=(-1)>退回上一步</a> ifufp=upfilename(ubound(upfilename))//保存的文件名称 (../upload/&ufp)//文件的保存路径%><script >_+=upload/<%=ufp%>()</script><%set file=nothingset upload=nothingset my_conn = nothingset rs = nothing%>这样应该可以了,如果还不行就email我,我的电子信箱地址是,我重新改过了,你去测试吧
发表评论