您是否曾经想过通过某种形式上传文件并想到:“我真的很想使用ASP,它确实具有该功能,但是我用来学习ASP的教程却没有提到如何做到这一点。” 您是否在四处寻找简单的解决方案,但又不想浏览复杂代码的页面? 您是否愿意为可能对您的特定项目而言过高的预制解决方案付费?
我想向您介绍这里的基本步骤。 是的,这非常简单,是的,您仅需使用ASP即可完成所有操作。 直接吗? 不完全正确,但是如果您了解操纵长字符串和使用scripting.fileSystemObject的基础,那么可以做到这一点。
注意
大部分代码已改编自的文章
visualBuilder.com ,用于上传和显示文件。
我使代码更加线性,并添加了保存文件的子例程。
步骤1:设定
首先,在实际进行上传之前,需要做好两件事。 您需要一个表格,并且需要一个文件夹。 “文件夹”是指您需要一个匿名Web用户有权保存文件的文件夹。 我建议这是主Web目录的子文件夹,并且不授予IIS权限从该文件夹执行脚本(以帮助防止恶意代码上传)。 在我的示例中,我在根Web目录的正下方使用了一个名为“ temp”的文件夹。
通过“表单”,您可能会认为我太过明显了,但是实际上可能需要对基本表单进行一些更改才能接受文件上传。 首先是表单标签的更改:
<form
action="upload.asp" method="post" enctype="multipart/form-data">
注意“ enctype”属性。
如果您没有将此属性设置为“ multipart / form-data”,则仅文件名将发送到处理程序,在我的情况下为“ upload.asp”。
然后,当然您需要输入type =“ file”才能接受上传:
<input type="file" name="myFileToUpload" accept="image/*">
请注意,您可以使用可选的“ accept”属性来过滤掉不可接受的文件类型。 我已将其设置为仅接受具有图像MIME类型的文件。 当然,这可以完全扩展或省略。 以您想要的其他任何方式完成表格,然后继续进行下一步。 步骤2:打开通过表单发布的二进制数据
与您过去可能使用过的常规格式输入不同,文件是作为二进制数据发送的,不能完全像字符串一样进行操作。 同样,不同的浏览器以略有不同的方式发送这些文件,因此打开所有发布的数据并在其中搜索以找出不同输入如何分开,然后尝试找出哪个文件是最容易的。 请注意,在处理二进制数据时,所使用的函数与字符串操作函数非常相似,只不过它们都以字母“ B”结尾。 它们的工作原理相同,但是它们只能处理二进制数据。 尝试这个:
<%
Dim posi, allData, delimiter, newLineB
'put the whole form posted into "allData"
allData = request.BinaryRead(Request.TotalBytes)
'find the first new line character in allData
newLineB = chrB(13) & chrB(10)
posi = instrB(1, allData, newLineB)
'find the string which separates the different inputs
delimiter = midB(allData, 1, posi-1)
'remove first delimiter and add a new line character to the end
allData = midB(allData, posi + 2, lenB(allData) - lenB(delimiter) - 2 - 4)
allData = allData & newLineB %>
步骤3:查找文件数据:
您现在拥有的是一个名为'delimiter'的变量,该变量保存浏览器用来分隔不同表单输入的分隔符(测试时,我使用了firefox,并在将其转换为文本后编写了该变量。事实证明这很长一连串的破折号后跟一个13位数字(我不知道这是什么意思)和一个称为“ allData”的文件,其将所有其余数据发布到处理程序中。 如果要将这些数据转换为字符串并将其写入屏幕,它将看起来像这样:
Content-Disposition: form-data; name="myTextInput"
hello world
-----------------------------4564239462453
Content-Disposition: form-data; name="myFileToUpload"; filename="pic.jpg"
Content-Type: image/jpeg
*** a whole bunch of nonsense characters representing all the binary data of the file ***
-----------------------------4564239462453
Content-Disposition: form-data; name="submit"
submit
-----------------------------4564239462453--
那么,我如何看待呢?
首先请注意,每个输入的第一行是内容配置。
这包括文件名,所以不要扔掉它。
第二行列出了内容类型,但除非此输入为文件,否则为空白。
因此,除非第二行具有内容类型,否则我将滚动浏览输入并将其丢弃。
我将使用一个将二进制数据转换为等效的ascii字符的函数,以及一个用于保存文件的子例程。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML><head><title>asp uploader</title></head><body>
<%
'find the file input, discard all others
dim lstart, lfinish, disposition, fileType, content
do while lenB(allData) > 0 'we're going to whittle allData down to nothing
lstart = 1
lfinish = instrB(1, allData, newLineB)
disposition = converter(midB(allData, lstart, lfinish - lstart))
lstart = lfinish + 2
If chr(ascB(midB(allData, lstart, 1))) = "C" then 'this input is a file
lfinish = instrB(lstart, allData, newLineB & newLineB)
'search for 2 new line characters, meaning the end of the
'content-type, saves it as fileType
fileType = trim(converter(midB(allData, lstart, lfinish - lstart)))
'set the rest of this input as 'content'
posi = instrB(allData, newLineB & delimiter & newLineB)
content = midB(allData, lfinish + 4, posi-lfinish-4)
'display data for the file
response.write "<b>Content Disposition</b><br>" & vbNewLine
response.write disposition
response.write "<br>" & vbNewLine
response.write "<b>Content Type</b><br>" & vbNewline
response.write fileType
response.write "<br>" & vbNewLine
response.write "<b>Content</b><br>"
'save file AND display it as either an <img> or <textarea>
saveFile content, disposition, fileType
Response.Write "<br>"
Response.Write "<br>"& vbNewLine
End If
'find the next delimiter in order to cut the first input from allData
posi = instrB(1, allData, newLineB & delimiter & newLineB)
allData = midB(allData, posi + 2 + lenB(delimiter) + 2)
Loop
Function converter(toConvert)
Dim output
Dim x
x = 1
output = ""
'convert one character at a time to Ascii and add it to the output
do while x <= lenB(toConvert)
output = output & chr(ascB(midB(toConvert, x, 1)))
x = x + 1
loop
converter = output
end function
sub saveFile(content, disp, typ)
dim objFSO, objTXT, path, fileName
'build the path to save the file
path = request.serverVariables("appl_physical_path") & "temp"
'sometimes the filename has "\" which affects how I save it
if instr(disp, "\") > 0 then
fileName = mid(disp, instrRev(disp, "\"), len(disp)-instrRev(disp, "\"))
else
fileName = "\" & mid(disp, instr(disp, "filename=")+10, len(disp)-instr(disp, "filename=")-10)
end if
path = path & fileName
'save file with normal FSO and textStream methods
set objFSO = server.createObject("scripting.fileSystemObject")
set objTXT = objFSO.openTextFile (path, 2, True)
objTXT.write converter(content)
response.write "<br>(File saved as: " & path & ")<br>" & vbNewLine
'display the file
if left(typ, 19) = "Content-Type: image" then 'file is an image
'write an image tag to the browser
response.write "<img src='/temp" & fileName & "'>"&vbNewLine
else 'file isn't an image
'write the contents of the file to a textarea in the browser
response.write "<textarea rows='10' cols='50' editable='false'>"
response.binaryWrite content
response.write "</textarea>" & vbNewline
end if
end sub
%>
</body>
请注意,我将文件的内容转换为ASCII字符,并使用文本流进行保存。
是的,这确实适用于二进制文件。
我用几个图像文件进行了测试。
有一个名为ADO.STREAM的单独对象,该对象应该用于在二进制文件中移动,但是我无法在该应用程序中使用它,并且由于普通ol'textstream正常运行,因此我将其保留为这个。
请给我任何意见。 我期待任何反馈。
杰瑞德