v 您现在的位置:八十年代交流网 ->>代 码 区》版   文字大小:[][][默认]

v 第1/2页
跳至
[刷新][下一页][尾页]

主题: [顶楼] 上传图片的ASP文件源码
邮寄到我的邮箱
打印本页
收藏本贴

作者:西帅(fynewsun)发表于2003-4-11 10:25:01

<%@LANGUAGE="VBSCRIPT"%>
<%
'*** File Upload to: ../temp_img, Extensions: "GIF,JPG,JPEG,BMP,PNG", Form: form1, Redirect: "", "file", "50000", "error"
'*** Pure ASP File Upload Modify Version by xPilot-----------------------------------------------------
' Copyright 2000 (c) George Petrov
'
' Script partially based on code from Philippe Collignon
' (http://www.asptoday.com/articles/20000316.htm)
'
' New features from GP:
' * Fast file save with ADO 2.5 stream object
' * new wrapper functions, extra error checking
' * UltraDev Server Behavior extension
'
' Copyright 2001-2002 (c) Modify by xPilot
' *** Date: 12/15/2001 ***
' *** 支持所有双字节文件名,而且修复了原函数中遇到空格也会自动截断文件名的错误! ***
' *** 保证百分百以原文件名保存上传文件!***
' *** Welcome to visite pilothome.yeah.net or mail [email protected] to me!***
'
' Version: 2.0.1 Beta for GB2312,BIG5,Japan,Korea ...
'------------------------------------------------------------------------------
Sub BuildUploadRequest(RequestBin,UploadDirectory,storeType,sizeLimit,nameConflict)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
'Check ADO Version
set checkADOConn = Server.CreateObject("ADODB.Connection")
adoVersion = CSng(checkADOConn.Version)
set checkADOConn = Nothing
if adoVersion < 2.5 then
Response.Write "<b>You don't have ADO 2.5 installed on the server.</b><br>"
Response.Write "The File Upload extension needs ADO 2.5 or greater to run properly.<br>"
Response.Write "You can download the latest MDAC (ADO is included) from <a href=""www.microsoft.com/data"">www.microsoft.com/data</a><br>"
Response.End
end if
'Check content length if needed
Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
If "" & sizeLimit <> "" Then
sizeLimit = CLng(sizeLimit)
If Length > sizeLimit Then
Request.BinaryRead (Length)
Response.Write "Upload size " & FormatNumber(Length, 0) & "B exceeds limit of " & FormatNumber(sizeLimit, 0) & "B"
Response.End
End If
End If
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
'Members variable of objects are put in a dictionary object
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")
'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
Pos = InstrB(Pos,RequestBin,getByteString("name="))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
FileName = Mid(FileName,InStrRev(FileName,"\")+1)
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = FileName
ValueBeg = PosBeg-1
ValueLen = PosEnd-Posbeg
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
ValueBeg = 0
ValueEnd = 0
End If
'Add content to dictionary object
UploadControl.Add "Value" , Value
UploadControl.Add "ValueBeg" , ValueBeg
UploadControl.Add "ValueLen" , ValueLen
'Add dictionary object to main dictionary
UploadRequest.Add name, UploadControl
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop

GP_keys = UploadRequest.Keys
for GP_i = 0 to UploadRequest.Count - 1
GP_curKey = GP_keys(GP_i)
'Save all uploaded files
if UploadRequest.Item(GP_curKey).Item("FileName") <> "" then
GP_value = UploadRequest.Item(GP_curKey).Item("Value")
GP_valueBeg = UploadRequest.Item(GP_curKey).Item("ValueBeg")
GP_valueLen = UploadRequest.Item(GP_curKey).Item("ValueLen")

if GP_valueLen = 0 then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
Response.Write "File does not exists or is empty.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
response.End
end if

'Create a Stream instance
Dim GP_strm1, GP_strm2
Set GP_strm1 = Server.CreateObject("ADODB.Stream")
Set GP_strm2 = Server.CreateObject("ADODB.Stream")

'Open the stream
GP_strm1.Open
GP_strm1.Type = 1 'Binary
GP_strm2.Open
GP_strm2.Type = 1 'Binary

GP_strm1.Write RequestBin
GP_strm1.Position = GP_ValueBeg
GP_strm1.CopyTo GP_strm2,GP_ValueLen

'Create and Write to a File
GP_curPath = Request.ServerVariables("PATH_INFO")
GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & UploadDirectory)
if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
GP_curPath = GP_curPath & "/"
end if
GP_CurFileName = UploadRequest.Item(GP_curKey).Item("FileName")
GP_FullFileName = Trim(Server.mappath(GP_curPath))& "\" & GP_CurFileName
'Check if the file alreadu exist
GP_FileExist = false
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(GP_FullFileName)) Then
GP_FileExist = true
End If
if nameConflict = "error" and GP_FileExist then
Response.Write "<B>File already exists!</B><br><br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
GP_strm1.Close
GP_strm2.Close
response.End
end if
if ((nameConflict = "over" or nameConflict = "uniq") and GP_FileExist) or (NOT GP_FileExist) then
if nameConflict = "uniq" and GP_FileExist then
Begin_Name_Num = 0
while GP_FileExist
Begin_Name_Num = Begin_Name_Num + 1
GP_FullFileName = Trim(Server.mappath(GP_curPath))& "\" & fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
GP_FileExist = fso.FileExists(GP_FullFileName)
wend
UploadRequest.Item(GP_curKey).Item("FileName") = fso.GetBaseName(GP_CurFileName) & "_" & Begin_Name_Num & "." & fso.GetExtensionName(GP_CurFileName)
UploadRequest.Item(GP_curKey).Item("Value") = UploadRequest.Item(GP_curKey).Item("FileName")
end if
on error resume next
GP_strm2.SaveToFile GP_FullFileName,2
if err then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "<br>"
Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
err.clear
GP_strm1.Close
GP_strm2.Close
response.End
end if
GP_strm1.Close
GP_strm2.Close
if storeType = "path" then
UploadRequest.Item(GP_curKey).Item("Value") = GP_curPath & UploadRequest.Item(GP_curKey).Item("Value")
end if
on error goto 0
end if
end if
next

End Sub

'把普通字符串转成二进制字符串函数
Function getByteString(StringStr)
getByteString=""
For i = 1 To Len(StringStr)
XP_varchar = mid(StringStr,i,1)
XP_varasc = Asc(XP_varchar)
If XP_varasc < 0 Then
XP_varasc = XP_varasc + 65535
End If

If XP_varasc > 255 Then
XP_varlow = Left(Hex(Asc(XP_varchar)),2)
XP_varhigh = right(Hex(Asc(XP_varchar)),2)
getByteString = getByteString & chrB("&H;" & XP_varlow) & chrB("&H;" & XP_varhigh)
Else
getByteString = getByteString & chrB(AscB(XP_varchar))
End If
Next
End Function

'把二进制字符串转换成普通字符串函数
Function getString(StringBin)
getString =""
Dim XP_varlen,XP_vargetstr,XP_string,XP_skip
XP_skip = 0
XP_string = ""
If Not IsNull(StringBin) Then
XP_varlen = LenB(StringBin)
For i = 1 To XP_varlen
If XP_skip = 0 Then
XP_vargetstr = MidB(StringBin,i,1)
If AscB(XP_vargetstr) > 127 Then
XP_string = XP_string & Chr(AscW(MidB(StringBin,i+1,1) & XP_vargetstr))
XP_skip = 1
Else
XP_string = XP_string & Chr(AscB(XP_vargetstr))
End If
Else
XP_skip = 0
End If
Next
End If
getString = XP_string
End Function

Function UploadFormRequest(name)
on error resume next
if UploadRequest.Item(name) then
UploadFormRequest = UploadRequest.Item(name).Item("Value")
end if
End Function

'Process the upload
UploadQueryString = Replace(Request.QueryString,"GP_upload=true","")
if mid(UploadQueryString,1,1) = "&" then
UploadQueryString = Mid(UploadQueryString,2)
end if

GP_uploadAction = CStr(Request.ServerVariables("URL")) & "?GP_upload=true"
If (Request.QueryString <> "") Then
if UploadQueryString <> "" then
GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
end if
End If

If (CStr(Request.QueryString("GP_upload")) <> "") Then
GP_redirectPage = ""
If (GP_redirectPage = "") Then
GP_redirectPage = CStr(Request.ServerVariables("URL"))
end if

RequestBin = Request.BinaryRead(Request.TotalBytes)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin, "../temp_img", "file", "500000", "error"

'*** GP NO REDIRECT
end if
if UploadQueryString <> "" then
UploadQueryString = UploadQueryString & "&GP;_upload=true"
else
UploadQueryString = "GP_upload=true"
end if


%>
<html>
<head>
<title>上传您的图片</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../images/css/tz.css" type="text/css">
<script language="JavaScript">
<!--
function getFileExtension(filePath) { //v1.0
fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length));
return fileName.substring(fileName.lastIndexOf('.')+1,fileName.length);
}

function checkFileUpload(form,extensions) { //v1.0
document.MM_returnValue = true;
if (extensions && extensions != '') {
for (var i = 0; i<form.elements.length; i++) {
field = form.elements[i];
if (field.type.toUpperCase() != 'FILE') continue;
if (field.value == '') {
alert('文件框中必须保证已经有文件被选中!');
document.MM_returnValue = false;field.focus();break;
}
if (extensions.toUpperCase().indexOf(getFileExtension(field.value).toUpperCase()) == -1) {
alert('这种文件类型不允许上传!.\n只有以下类型的文件才被允许上传: ' + extensions + '.\n请选择别的文件并重新上传.');
document.MM_returnValue = false;field.focus();break;
} } }
}

function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" enctype="multipart/form-data" action="<%=GP_uploadAction%>" onSubmit="checkFileUpload(this,'GIF,JPG,JPEG,BMP,PNG');return document.MM_returnValue" method="post">
<table width="350" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<td bgcolor="#0099FF">
<table width="100%" border="0" cellspacing="1" cellpadding="4" class="pt9">
<tr bgcolor="#EEEEEE" align="center">
<td colspan="2" class="pt9-000099" height="35"> 上传您的图片</td>
</tr>
<tr bgcolor="#EEEEEE">
<td width="17%" bgcolor="#EEEEEE" align="center">图片</td>
<td width="83%">
<input type="file" name="file" class="txtfld">
</td>
</tr>
<tr bgcolor="#EEEEEE">
<td width="17%"> </td>
<td width="83%">
<input type="submit" value=" 上 传 " class="txtfld4">
<% if request.querystring ("GP_upload") <> "" then %>
<script>window.opener.form1.f3_content.value+='http://www.dw-mx.com/forum/temp_img/<%=UploadFormRequest("file")%>';window.close();</script><% end if %>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>


-----------------------
注意:下面的js做一下说明
<script>window.opener.form1.f3_content.value+='http://www.dw-mx.com/forum/temp_img/<%=UploadFormRequest("file")%>';window.close();</script>

form1:指将上传的文件名返回原页面的表单名
f3_content:指表单中的控件名
后面的地址是你指定上传的目录,一般要用只要修改这几个地方就可以,记住这个js是针对弹出窗口的上传页面用的,就好比我论坛上的上传图片样子,否则js失效。

[留言] [回复]
------------------------------
一些事是永远都无法明白的
一些人是永远都无法忘记的

/bbs/photo/20031224195539.gif


主题: [2楼] [回复<%@LANGUAG]文雨回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:文雨(litaifeng)发表于2004-2-23 18:58:14

[文雨<%@LANGUAG道:“讲正经事,言归正传吧!”]
如果服务器的操作系统是英文版的,在运行"文件上传"对话框时,点击"提交"时,会出现错误,是什么原因?("文件上传"对话框的源代码同上)
比如:
XP_string = XP_string & Chr(AscW(MidB(StringBin,i+1,1) & XP_vargetstr)) 这条语句,提示Chr没定义或非法!!!

[留言] [回复]
------------------------------
网络是虚拟的,我是现实的


主题: [3楼] [回复文雨]西帅回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:西帅(fynewsun)发表于2004-2-23 19:09:53

应该不会是chr的问题,这是ASP内嵌的函数,不过现在可以使用上传组件实现上传,而不需要这么麻烦。

[留言] [回复]
------------------------------
一些事是永远都无法明白的
一些人是永远都无法忘记的

/bbs/photo/20031224195539.gif


主题: [4楼] [回复而不需要这么麻烦]文雨回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:文雨(litaifeng)发表于2004-2-25 17:16:45

能提供用组件上传文件的源代码吗?

[留言] [回复]
------------------------------
网络是虚拟的,我是现实的


主题: [5楼] [回复文雨]西帅回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:西帅(fynewsun)发表于2004-2-25 17:46:00

你可以到站点公务“意见互动”版里找交流网以前版本的源文件,下载下来看看,不过你的机器要安装fileup组件,我这边有光碟,不过不好给你。

[留言] [回复]
------------------------------
一些事是永远都无法明白的
一些人是永远都无法忘记的

/bbs/photo/20031224195539.gif


主题: [6楼] [回复西帅]Re:西帅回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:文雨(litaifeng)发表于2004-2-25 17:55:04

文件有多大?可以发送给我吗?

[留言] [回复]
------------------------------
网络是虚拟的,我是现实的


主题: [7楼] [回复文雨]西帅回复:
邮寄到我的邮箱
打印本页
收藏本贴

作者:西帅(fynewsun)发表于2004-2-25 17:59:10

邮件传不过去,6M多。

[留言] [回复]
------------------------------
一些事是永远都无法明白的
一些人是永远都无法忘记的

/bbs/photo/20031224195539.gif


主题: [8楼] [回复西帅]Re:上传图片的ASP文件源码
邮寄到我的邮箱
打印本页
收藏本贴

作者:蓝の羽(litao2001cn)发表于2004-3-3 11:39:10

看着看着就头大了,怎么多啊?不过好在前面的一点能看得懂,中间的那段二进三进的就不懂了。。80spp/008.gif

[留言] [回复]
------------------------------
我是那--
在你许愿时
划过天际的那颗流星
燃烧我的生命,给你一刻的灿烂
带着你的祝福
到天堂
把我赤裸的灵魂
交给天使,换你一生的快乐


v 第1/2页
跳至
[刷新][下一页][尾页]

v 您现在的位置:八十年代交流网 ->>代 码 区》版
[返回]

公司简介 - 相关条款 - 站点地图 - 合作信息 - 网站建设 - 广告业务
Copyright  ©  2002-2004  八十年代·版权所有