ASP 如何上传图片?

作者&投稿:超晓 (若有异议请与网页底部的电邮联系)
ASP中上传图片怎么做啊?~

采用无组件下载代码。
upfile.asp





<%
dim upload,file,formName,formPath,iCount,MyFile
set upload=new upload_5xSoft ''建立上传对象

formpath="userdata/" ''得到上传目录

nian=Year(Date)
yue=Month(Date)
ri=Day(Date)
if len(nian)<4 then nian="00"&nian
if len(yue)<2 then yue="0"&yue
if len(ri)<2 then ri="0"&ri

ipinit=""
ip=Request.ServerVariables("REMOTE_ADDR")
tmpip=Split(ip,".")
tmpipb=UBound(tmpip)
' ReDim ThisIP(tmpipb)
for i=0 to tmpipb
ThisIp=tmpip(i)

Do WHile len(ThisIP)<3
ThisIP="0"+ThisIP
loop
ipinit=ipinit+ThisIP
next

'Response.write nian&yue&ri&ipinit
'Response.end

for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
'MyFile=formPath&File.FileName
'MyFile=File.FileName


shi=Hour(time)
fen=Minute(Time)
Miao=Second(Time)
if len(shi)<2 then shi="0"&shi
if len(fen)<2 then fen="0"&fen
if len(miao)<2 then miao="0"&miao

MyFile=nian&yue&ri&shi&fen&miao&ipinit&".txt"

file.SaveAs Server.mappath(formPath&MyFile) ''保存文件
response.write file.FilePath&"#"&file.FileName&"#"&" ("&file.FileSize&") => "&MyFile&" 成功!"
end if
set file=nothing
next
set upload=nothing ''删除此对象

%>


upload_5xsoft.inc




dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize
dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
Version="HTTP上传程序 Version 1.0"
if Request.TotalBytes<1 then Exit Sub
set Form=CreateObject("Scripting.Dictionary")
set File=CreateObject("Scripting.Dictionary")
set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
upfile_5xSoft_Stream.mode=3
upfile_5xSoft_Stream.type=1
upfile_5xSoft_Stream.open
upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)&Chr(10)
iDivLen=inString(1,vbEnter)+1
strDiv=subString(1,iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStart,strDiv)-1
while iFormStart < iFormEnd
iStart=inString(iFormStart,"name=""")
iEnd=inString(iStart+6,"""")
mFormName=subString(iStart+6,iEnd-iStart-6)
iFileNameStart=inString(iEnd+1,"filename=""")
if iFileNameStart>0 and iFileNameStart<iFormEnd then
iFileNameEnd=inString(iFileNameStart+10,"""")
mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)
if iEnd>iStart then
mFileSize=iEnd-iStart-4
else
mFileSize=0
end if
set theFile=new FileInfo
theFile.FileName=getFileName(mFileName)
theFile.FilePath=getFilePath(mFileName)
theFile.FileSize=mFileSize
theFile.FileStart=iStart+4
theFile.FormName=FormName
file.add mFormName,theFile
else
iStart=inString(iEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)

if iEnd>iStart then
mFormValue=subString(iStart+4,iEnd-iStart-4)
else
mFormValue=""
end if
form.Add mFormName,mFormValue
end if

iFormStart=iformEnd+iDivLen
iFormEnd=inString(iformStart,strDiv)-1
wend
End Sub

Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=""
for i=1 to theLen
if upfile_5xSoft_Stream.EOS then Exit for
c=ascB(upfile_5xSoft_Stream.Read(1))
If c > 127 Then
if upfile_5xSoft_Stream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function

Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
if i>upfile_5xSoft_Stream.size then exit Function
upfile_5xSoft_Stream.Position=i-1
if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if upfile_5xSoft_Stream.EOS then
inString=0
Exit for
end if
if AscB(upfile_5xSoft_Stream.Read(1))AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString0 then Exit Function
end if
next
End Function

Private Sub Class_Terminate
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
upfile_5xSoft_Stream.close
set upfile_5xSoft_Stream=nothing
End Sub


Private function GetFilePath(FullPath)
If FullPath "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function

Private function toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=""
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
End Class


Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=1
if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
if FileStart=0 or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
upfile_5xSoft_Stream.position=FileStart-1
upfile_5xSoft_Stream.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class

例子:SQL Server数据库
create database a
create table images
(
id int identity(1,1) primary key,

shopImages image

)

创建两个web窗体:
第一个:









</body
按钮单击事件里:
添加引用:using System.Data;
using System.Data.SqlClient;

using System.IO;

try
{
if (FileUpload1.HasFile)
{
string name = FileUpload1.FileName;
string names = name.Substring(name.LastIndexOf(".") + 1);
if (names == "jpg" || names == "gif" || names == "png")
{
//取得上传文件的大小
int FileLen = FileUpload1.PostedFile.ContentLength;

Byte[] FileData = new Byte[FileLen];

//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpload1.PostedFile;

//创建数据流对象
Stream sr = hp.InputStream;

//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);

SqlConnection conn = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into a(image) values(@img)", conn);

//存储过程插入到数据库中
SqlParameter para = new SqlParameter("@img", SqlDbType.Binary);
para.Value = FileData;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
//关闭数据库连接
conn.Close();
//弹出上传成功的提示
Response.Write("alert('上传成功');");

}
else
{
Response.Write("alert('上传文件格式不对,只允许上传jpg和gif格式的文件');");
}
}
else
{
Response.Write("alert('请选择上传文件');");
}
}
catch
{
}

第二个web窗体:
页面加载事件:
int id = Convert.ToInt32(Request.QueryString["id"]);
using (SqlConnection conn = new SqlConnection("server=.;database=a;uid=sa;pwd=123456"))
{
using (SqlCommand comm = conn.CreateCommand())
{
comm.CommandText = "select imagesfrom shopImages id=@id";
comm.Parameters.AddWithValue("@id", id);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader != null)
{
reader.Read();
byte[] photo = null;
if (reader[0] != null)
{
photo = (byte[])reader[0];
}
Response.ContentType = "image/jpeg";
Response.BinaryWrite(photo);
Response.End();
}
}
}

给你个简单的例子:
1.摆上3个控件。
<asp:FileUpload ID="FileUpload1" runat="server" Width="220px" />
<asp:Image ID="Image3" runat="server" Height="120px" Width="120px" />

<asp:Button ID="Button3" runat="server" Text="保存" Width="48px" onclick="Button3_Click" />

2.双击button
if (FileUpload1.HasFile == true)//HasFile用来检查FileUpload是否有指定文件
{
string Image = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (Image != ".bmp" && Image != ".png" && Image != ".gif" && Image != ".jpg")// 这里你自己加入其他图片格式,最好全部转化为大写再判断,我就偷懒了
{
Response.Write("<script>alert('请选择图片!')</script>");
return; // 这样用return 避免太多的嵌套
}
string filename = FileUpload1.FileName;
string savePath = Server.MapPath(("./imges/") + filename);//你的存放路径
FileUpload1.SaveAs(savePath);// 把照片存入了文件夹
url = "imges/" + filename;
Image3.ImageUrl = url;// 显示图片~ 就ok了。

}
这只是个最简单的方式 你先了试试 看能不能实现。

利用这个组件就可以实现上传了,里面已经写好了上传代码,你直接使用即可。




小金县18445169999: ASP中如何实现图片的上传 -
岛蕊安迪: 你在文件里找到login,后缀改成htm 登录名一般是admin 进到后台就可以添加本地图片

小金县18445169999: asp,最简单的上传图片
岛蕊安迪: 1.如果服务器装了文件上传组件,可查阅一下该组件的相关属性和方法,不过现在很多服务器空间都不支持这类组件.暂不在这里讨论. 2.若服务器不支持文件上传组件,可以考虑自己写个无组件上传程序,网上有一个"化境无组件上传"比较经...

小金县18445169999: 怎么把图片上传到ASP空间里去?
岛蕊安迪: 用FTP上传工具如fxp上传到网站主目录里.有的是WEB 有的是WWWROOT.然后在你要显示的页面加入html代码显示图片可以用<img src="https://img.sogoucdn.com/v2/thumb/?appid=200698&url=%E5%9B%BE%E7%89%87%E8%B7%AF%E5%BE%84">来显示. 也可以直接打开图片的地址来显示图片. 如:你的网址为:http://www.myweb.com/ 你的图片 1.jpg 传在wwwroot目录里.就提交地址 http://www.myweb.com/1.jpg 就直接显示图片

小金县18445169999: asp上传图片
岛蕊安迪: 方式1:图片文件名不要用动态生成的随机名称,改用固定的名字,这样先按照图片名称删除原始图片,然后再上传新的图片. 方式2:继续使用随机名称,上传新图片前,从数据库中读取到原始图片的名字,然后删除,然后再上传新图片

小金县18445169999: 在ASP中如何实现上传图片? -
岛蕊安迪: 文件一upload_5xsoft.inc dim Data_5xsoft Class upload_5xsoft dim objForm,objFile,Version Public function Form(strForm) strForm=lcas...

小金县18445169999: asp如何实现上传图片 功能
岛蕊安迪:http://wenwen.sogou.com/z/q800719100.htm 这里有

小金县18445169999: 在ASP中,如何实现图片的上传,用数据库不用 -
岛蕊安迪: 一种方法:用组件上传.百度一下“ASP上传组件”应该能找到不少,用法也很简单,网上也有说明文件.还有一种方法是不用组件,用fso对象.如果你要记录这些数据,并在站点上显示,需要用到数据库,可以记录文件路径,也可以记录文件.SQL和Access数据库都有一个数据类型用来存储字段,可以查看SQL帮助文件找到.

小金县18445169999: ASP图片上传代码//急急急//
岛蕊安迪: &lt;% set upload=new upload_5xsoft set file=upload.file("sf_upfile") if file.fileSize&lt;1 then response.write"&lt;script language=javascript&gt;alert('您没有选择图片.~~~\n\n-----请点击浏览按钮,从弹出的窗口中选择要上传的图片.\n\n-----然后...

小金县18445169999: asp中怎么把图片上传到数据库,然后再显示出来
岛蕊安迪: 两种方法,一种是把图片以二进制流的形式存到数据库,然后把流输出到网页上,这种方法很占数据库资源,所以很少有人用.二,把图片上传到空间,然后把图片的路径存到数据库里.然后在网页上读到地址.(常用)

小金县18445169999: 急!!asp怎么上传图片到数据库,并显示到网页中 -
岛蕊安迪: 你到网上搜关键字[林子图片管理],他就是用的你说的这种方法,完整的,可以参考,

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网