70分送上,谁有完整的上传图片和资料的asp代码

littleBenhorse 2002-01-29 01:53:05
谁有完整的上传图片和资料的asp代码?要求图片传到指定目录下,如/pic/, 图片名称插入数据库中?急,谢了!70分送上。
...全文
146 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
haiznan 2002-01-30
  • 打赏
  • 举报
回复
上传一个jpg文件的示例:

文件1: upload.htm

<html><title>example</title>
<body>
<form name="form1" method="post" action="upload.asp" enctype="multipart/form-data">
<input type=file name="file1">
<input type=submit name="submit" value="提交">
</form>
</body>
</html>

文件2: upload.asp

<html><title>example</title>
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
set file=upload.file("file1")
if file.fileSize>0 then
file.saveAs Server.mappath("temp.jpg")
response.write "<br>上传文件:"&file.FileName&" => temp.jpg OK!"
response.write "<br>文件大小:"&file.FileSize
set file=nothing
end if
set upload=nothing
%></body>
</html>

2.列表出有文件表单(多文件上传)
<html><title>example</title>
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
for each formName in upload.file
set file=upload.file(formName)
if file.FileSize>0 then
file.SaveAs Server.mappath(file.FileName)
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "
response.write file.FileName&" 成功!<br>"
end if
set file=nothing
next
set upload=nothing
%>
littleBenhorse 2002-01-30
  • 打赏
  • 举报
回复
马上给
nova1980 2002-01-29
  • 打赏
  • 举报
回复
怎么没有见到给分呢??
littleBenhorse 2002-01-29
  • 打赏
  • 举报
回复
谢谢
littleBenhorse 2002-01-29
  • 打赏
  • 举报
回复
和和
希偌 2002-01-29
  • 打赏
  • 举报
回复
一直以来,由于FileSystemObject的局限,所以ASP最大的难题就是文件上传,大多解决法就是安装

第三方上传组件。可第三方组件有很多问题,有的组件要注册,有的组件要在表单中加上他的版权信息。

还有的就是组件的兼容问题。

在网上也流传了很多无组件上传的代码,但都是只能上传文本文件,或是只能将文件上传到数据库中。

我这段时间在研究ASP,发现可以不用第三方组件上传任意类型的文件。就写了这个类,给大家一

个方便,整个类放在一个文件中: upload_5xsoft.inc 在 Example 目录下还有一个完整的多文件上传示

例程序,可以直接使用。

申明:源代码是完全开放的,可能随意传播,但请保留其完整性,未经作者同意,不得用于商业。




运行平台与注意事项

a)只能运行于 Windows2000+IIS 5,不支持 NT4+IIS4 或是 Win98+PWS, 只要在ASP中加上:
<!--#include FILE="upload_5xsoft.inc"--> 就行了


b) 在使用文件上传时, 表单 form 要加上 enctype="multipart/form-data" 即:

<form name="form1" method="post" action="" enctype="multipart/form-data">
<input type="text" value="abc" name="text1">
<input type=file name="file">
<input type=submit name="submit" value="提交">
</form>




upload_5xsoft的对象

如定义一个上传对象
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft 'upload就是一个对象
%>

upload_5xsoft 对象成员
file 文件对象集,(是个dictionary对象)

文件对象成员:
Count 属性,文件表单的个数
FileName 属性,上传文件的名字
FileSize 属性,上传文件的大小(为0是表示没有文件)
FilePath 属性,上传前文件所在的路径
FormName 属性,文件表单的名字
SaveAs 方法,储存上传后文件,有一个参数,路径要为真实路径如:
例子: set file=upload.file("file1") 'file1为表单名

response.write "<br>文件名:"&file.FileName

response.write "<br>文件大小:"&file.FileSize

response.write "<br>文件路径:"&file.FilePath

file.saveAs Server.mappath("/1.jpg")

set file=nothing
form 表单数据集,(是个dictionary对象)用来代替 Request.Form
count 属性,表单数
exists 方法,检查是否有指定的表单名
更多的用法可看 vbscript 的dictionary对象帮助
例子:
'得到text1表单的数据,uplaod就是一开始创建的对象

sText=upload.form("text1")
Version 属性,upload_5xsoft类的版本号,如:

response.write upload.Version




使用示例

1.上传一个jpg文件的示例:

文件1: upload.htm

<html><title>example</title>
<body>
<form name="form1" method="post" action="upload.asp" enctype="multipart/form-data">
<input type=file name="file1">
<input type=submit name="submit" value="提交">
</form>
</body>
</html>

文件2: upload.asp

<html><title>example</title>
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
set file=upload.file("file1")
if file.fileSize>0 then
file.saveAs Server.mappath("temp.jpg")
response.write "<br>上传文件:"&file.FileName&" => temp.jpg OK!"
response.write "<br>文件大小:"&file.FileSize
set file=nothing
end if
set upload=nothing
%></body>
</html>

2.列表出有文件表单(多文件上传)
<html><title>example</title>
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
for each formName in upload.file
set file=upload.file(formName)
if file.FileSize>0 then
file.SaveAs Server.mappath(file.FileName)
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "
response.write file.FileName&" 成功!<br>"
end if
set file=nothing
next
set upload=nothing
%>
kxcc_sx 2002-01-29
  • 打赏
  • 举报
回复
我有要吗?可以上传声音,图象,视频三种文件
第一个文件file_up.asp
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=big5">

<STYLE type=text/css>
TD {
FONT-FAMILY: "宋體"; FONT-SIZE: 12px
}
BODY {
FONT-FAMILY: "宋體"; FONT-SIZE: 9pt
}
SELECT {
FONT-FAMILY: "宋體"; FONT-SIZE: 9pt
}
A {
COLOR: #ff0000; FONT-FAMILY: "宋體"; FONT-SIZE: 9pt; TEXT-DECORATION: none
}

A:hover {
FONT-SIZE: 9pt; COLOR: #0000ff; FONT-FAMILY: "宋體"}
.unnamed1 { line-height: 25px}
</STYLE>


<DIV align="left">
<FORM METHOD="POST" ACTION="upload.asp" ENCTYPE="multipart/form-data"name="frmfile" target="_blank">
<FIELDSET><LEGEND><font color="#FFFFFF">試題相關媒體文件上傳</font></LEGEND>
<UL>
<LI><font color="#FFFFFF">歡迎使用檔案(文件)上傳服務﹐您可以上傳多個檔案(文件) </font>
<LI><font color="#FFFFFF">注意﹐本上傳軟件不支持中文﹐所以檔案(文件名)名稱不能含有中文﹐否則會產生錯誤。 </font>
<LI><font color="#FFFFFF">上傳文件的類型﹕
<input type="radio" value="a" checked name="R1" onclick="getimg()">
圖片
<input type="radio" name="R1" value="b" onclick="getvideo()">
視頻文件
<input type="radio" name="R1" value="c" onclick="getsound()">
聲音文件 </font>
</UL>
<P align="center"><font color="#FFFFFF">
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50">
<BR>
<INPUT TYPE="button" VALUE="開始上傳文件" onclick="chfile()">
</font><br>
<input type="hidden" name="filepath" size="20">
<input type="hidden" name="uptype" size="20" value="a">
<input type="hidden" name="typelist" size="20"value="">
</FIELDSET>
</FORM>
</DIV>
</body>
</html>
<script language=vbscript>
<!--
'******************************************
'函數名﹕gtfilename 參數類型﹕string
'作用﹕將從包含文件名的路徑中將文件名提
'取出來
'編寫時間﹕2001.11.12
'******************************************
function gtfilename(string1)
string1=trim(string1)
str_len=len(string1)
if string1<>empty then
s1=instr(string1,"\")
string1=mid(string1,s1+1,str_len-s1)
if instr(string1,"\")=0 then
gtfilename=string1
else
gtfilename=gtfilename(string1)
end if
end if
end function
sub getimg()
frmfile.uptype.value="a"
end sub
sub getvideo()
frmfile.uptype.value="b"
end sub
sub getsound()
frmfile.uptype.value="c"
end sub
sub chfile()
if frmfile.file1.value=empty then
window.alert "請選擇你要上載的文件﹗"
exit sub
else
if frmfile.filepath.value=empty then
frmfile.filepath.value=gtfilename(frmfile.file1.value)
else
frmfile.filepath.value=gtfilename(frmfile.file1.value)
end if
extname=right(trim(frmfile.file1.value),3)
extname=lcase(extname)
if extname="asp" then
window.alert "對不起﹗不能上傳擴展名為asp的文件﹗"
exit sub
end if
if extname="php" then
window.alert "對不起﹗不能上傳擴展名為php的文件﹗"
exit sub
end if
if extname="jsp" then
window.alert "對不起﹗不能上傳擴展名為jsp的文件﹗"
exit sub
end if
if extname="exe" then
window.alert "對不起﹗不能上傳擴展名為exe的文件﹗"
exit sub
end if
if extname="com" then
window.alert "對不起﹗不能上傳擴展名為com的文件﹗"
exit sub
end if
if extname="eml" then
window.alert "對不起﹗不能上傳擴展名為eml的文件﹗"
exit sub
end if
if extname="cih" then
window.alert "對不起﹗不能上傳擴展名為CIH的文件﹗"
exit sub
end if
if frmfile.uptype.value="a" then
if instr("gif#jpg#bmp#ico#",extname)=0 then
window.alert "對不起﹗你上傳的不是圖片文件﹗"
exit sub
end if
end if
if frmfile.uptype.value="b" then
if instr("avi#wmv#mpeg#mpg#m1v#wma#asf#",extname)=0 then
window.alert "對不起﹗你上傳的不是視頻文件﹗"
exit sub
end if
end if
if frmfile.uptype.value="c" then
if instr("mid#midi#rmi#wav#snd#au#mp3",extname)=0 then
window.alert "對不起﹗你上傳的不是聲音文件﹗"
exit sub
end if
end if
if frmfile.typelist.value=empty then
frmfile.typelist.value=frmfile.uptype.value
else
frmfile.typelist.value=frmfile.uptype.value
end if
frmfile.submit
end if
end sub
-->
</script>
第2个文件
upload.asp
<%
Dim objUpload
Dim FileCount
'建立 AspSmartUpload 對象
Set objUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
'使用 AspSmartUpload 對象的 Upload 方法﹐取得表單送出來的數據
objUpload.Upload
upfilename=objUpload.files.item(1).filename
%>
<!--#include file="test_upload.asp"-->
<%
'將文件以原文件名存入指定的虛擬路徑中﹐並將上傳的文件數存入 FileCount 變量中
FileCount = objUpload.Save("Upload Files")
Filename=objUpload.files.item(1).filename
Flieext=objUpload.files.item(1).fileext
Filesize=objUpload.files.item(1).size
appath1="Upload Files/"&Filename
flag1=objUpload.Form("R1").values
'將文件列表寫入session變量
if session("upfile_name_list")="" and session("upfile_type_list")="" then
session("upfile_name_list")=objUpload.Form("filepath").values
session("upfile_type_list")=objUpload.Form("typelist").values
else
session("upfile_name_list")=session("upfile_name_list")&"#"&objUpload.Form("filepath").values
session("upfile_type_list")=session("upfile_type_list")&"#"& objUpload.Form("typelist").values
end if
k1=session("upfile_name_list")
k2= session("upfile_type_list")
response.write k1&"<br>"
response.write k2&"<br>"
if flag1="a" then
type1="圖形文件"
end if
if flag1="b" then
type1="視頻文件"
end if
if flag1="c" then
type1="聲音文件"
end if

%>
<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=big5">
<title>上傳文件處理</title>
</head>
<body>
<P ALIGN="CENTER"><IMG SRC="image\upload.jpg"></P>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="96%" id="AutoNumber1" height="135">
<tr>
<td width="56%" colspan="2" height="58" rowspan="2"><font color="#FF0000" size="5">
<% = FileCount %>個文件已經上傳完畢﹗</font></td>
<td width="64%" height="29">預覽播放媒體文件</td>
</tr>
<tr>
<td width="64%" height="25"><%=type1%></td>
</tr>
<tr>
<td width="46%" height="19" colspan="2">
<p align="center">上傳文件的信息</td>
<td width="74%" rowspan="5" height="76">
<p align="center">
<% if flag1="a" then %>
<img border="0" src="<%=appath1%>" width="100" height="100">
<%end if%>
<%if flag1="c" then %>
<embed width="300" height="45"src="<%=appath1%>">
<%end if%>
<%if flag1="b" then %>
<embed width="300" height="300"src="<%=appath1%>">
<noembed>對不起﹗你的IE不支持此種媒體形式﹗</noembed>
<%end if%>
</td>
</tr>
<tr>
<td width="21%" height="1">上傳的文件名﹕</td>
<td width="25%" height="1"><% = Filename %></td>
</tr>
<tr>
<td width="21%" height="1">文件的擴展名﹕</td>
<td width="25%" height="1"><% =Flieext%></td>
</tr>
<tr>
<td width="20%" height="2">文件的大小﹕</td>
<td width="26%" height="2"><% = Filesize %>字節</td>
</tr>
<tr>
<td width="46%" height="176" colspan="2">
<p align="center"><INPUT type="button" value="關閉本窗口" onclick="window.close()"></td>
</tr>
</table>

</body>

</html>
还有一个文件!算了
你可以把具体的要求给我,我给你量身定做一个
littleBenhorse 2002-01-29
  • 打赏
  • 举报
回复
我要
littleBenhorse 2002-01-29
  • 打赏
  • 举报
回复
没人
希偌 2002-01-29
  • 打赏
  • 举报
回复
http://www.csdn.net/Expert/topic/487/487240.shtm
ssm1226 2002-01-29
  • 打赏
  • 举报
回复
to beyond_xiruo(希偌):给我发一份?
ssm_2008@263.net
ssm1226 2002-01-29
  • 打赏
  • 举报
回复
组件上传还是纯asp
希偌 2002-01-29
  • 打赏
  • 举报
回复
给你一个上传的类和使用方法,要不?
littleBenhorse 2002-01-29
  • 打赏
  • 举报
回复
没人回?

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧