- <%@Language="VBScript"%>
- <SCRIPT language="VBScript" runat="Server">
- Dim strReturnString,sFileName,sRelativePath
- '为了增强安全性,开发者可增加验证当前用户的功能,例如通过Session等
- '防止用户自己写客户端脚本向savedoc.asp提交非法文件
- Function SaveFile(sFilePath,sContent)
- On Error Resume Next
- Dim oStream
- ''''''''''''''' 生成调试信息 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Response.write("<div> 3</ br></div>") ''
- Response.write("<div>Message : Begin Create ADODB.Stream ... </ br></div>") ''
- ''
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Set oStream = Server.CreateObject("ADODB.Stream")
- ''''''''''''''' 生成调试信息 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- If Err.number <> 0 Then ''
- Response.write("<div>Message : Create ADODB.Stream error : " & Err & "</ br></div>") ''
- Else ''
- Response.write("<div>Message : Create ADODB.Stream Success! </ br></div>" ) ''
- End If ''
- ''
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- oStream.Type = 1
- oStream.Open
- ''''''''''''''' 生成调试信息 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Response.write("<div> 4</ br></div>") ''
- Response.write("<div>Message : Begin read sContent to ADODB.Stream ...</ br></div> ") ''
- ''
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- oStream.Write sContent
- ''''''''''''''' 生成调试信息 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- If Err.number <> 0 Then ''
- Response.write("<div>Message : read sContent to ADODB.Stream error : " & Err & "</ br></div>")''
- Else ''
- Response.write("<div>Message : read sContent to ADODB.Stream Success! </ br></div>" ) ''
- End If ''
- Response.write("<div> 5</ br></div>") ''
- Response.write("<div>Message : Begin save ADODB.Stream to file ... </div>") ''
- ''
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- oStream.SaveToFile sFilePath, 2
- ''''''''''''''' 生成调试信息 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- If Err.number <> 0 Then ''
- Response.write("<div>Message : save ADODB.Stream to file error : " & Err & "</ br></div>") ''
- Else ''
- Response.write("<div>Message : save ADODB.Stream to file Success!</ br></div>" ) ''
- End If ''
- Response.write("<div> </ br></div>") ''
- ''
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- oStream.Close
- Set oStream = Nothing
- SaveFile = True
- If Err.number <> 0 Then
- SaveFile = False
- End If
- End Function
- Sub readAndSaveFile()
- On Error Resume Next
- Dim i
- Dim sFileContent
- Dim oXML
- ''''''''''''''' 生成调试信息 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Response.write("<div>Information : </ br></div>") ''
- Response.write("<div> 1</ br></div>") ''
- Response.write("<div>Message : Begin Create Msxml2.DOMDocument ... </ br></div>") ''
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Set oXML = Server.CreateObject("Msxml2.DOMDocument")
- ''''''''''''''' 生成调试信息 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- If Err.number <> 0 Then ''
- Response.write("<div>Message : Create Msxml2.DOMDocument error : " & Err & "</ br></div>") ''
- Else ''
- Response.write("<div>Message : Create Msxml2.DOMDocument Success! </ br></div>" ) ''
- End If ''
- Response.write("<div> 2</ br></div>") ''
- Response.write("<div>Message : Begin load Request ... </ br></div> ") ''
- ''
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- oXML.async = false
- oXML.load Request
- sFileContent = oXml.documentElement.childNodes.item(0).nodeTypedValue
- 'FileName和RelativePath区分大小写
- sFileName = oXml.documentElement.childNodes.item(0).Attributes.getNamedItem("FileName").Text
- sRelativePath= oXml.documentElement.childNodes.item(0).Attributes.getNamedItem("RelativePath").Text
- ''''''''''''''' 生成调试信息 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- If Err.number <> 0 Then ''
- Response.write("<div>Message : Load Request error : " & Err & "</ br></div>") ''
- Else ''
- Response.write("<div>Message : Create Msxml2.DOMDocument Success! </ br></div>" ) ''
- End If ''
- ''
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- '得到文件扩展名
- Dim intP, strFileExName
- intP = InStrRev(sFileName, ".")
- strFileExName = Mid(sFileName, intP)
- '接受doc、ppt和xls类型文件。开发者可增加接受其他类型文件
- if strFileExName = ".doc" or strFileExName = ".xls" or strFileExName = ".ppt" or strFileExName = ".wps" then
- SaveFile server.mappath(".") & "" & sRelativePath & sFileName, sFileContent
- end if
- Set oXml = Nothing
- If Err.number <> 0 Then
- strReturnString = "-1"
- End If
- End Sub
- '执行文件
- readAndSaveFile
- If strReturnString = "-1" Then
- Response.write("Save Error!")
- Else
- Response.write("Save OK.")
- End If
- </SCRIPT>