Please review the follow sample code to save the open Microsoft Office file in the EDraw Office Viewer Component to the remote server with the ASP code.
SaveDoc1.asp
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”936″%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>EDraw Office Viewer Component</title>
</head>
<body>
<%readonly int enterCount = 12;
string[] requestValues = new string[3];
string newFile = Server.MapPath(“.”)+”\\Document\\” + DateTime.Now.ToFileTime().ToString() + “NewDoc.doc”;
BinaryReader br = new BinaryReader(Request.InputStream);
br.BaseStream.Seek(0,SeekOrigin.Begin);
int enterNo = 0;
int streamHeadLen = 0;
while(br.BaseStream.Position < br.BaseStream.Length) {
streamHeadLen ++;
char c = (char)br.ReadByte();
if(enterNo < enterCount){
if(c == ‘\n’){
enterNo ++;
}
}
else{
break;
}
}
br.BaseStream.Seek(0,SeekOrigin.Begin);
string strTemp = System.Text.UTF8Encoding.Default.GetString(br.ReadBytes(streamHeadLen – 1));
string str =””;
int index=0;
string[] requestStrings = {“RecordID”,”UserID”};
for(int i=0;i<requestStrings.Length;i++)
{
str = “Content-Disposition: form-data; name=\””+requestStrings[i]+”\”\r\n\r\n”;
index = strTemp.IndexOf(str) + str.Length;
if( index != str.Length – 1)
{
for(int j=index;j<strTemp.Length;j++)
{
if(strTemp[j] != ‘\r’)
this.requestValues[i] += strTemp[j];
else
break;
}
}
}
str = “; filename=\”;
index = strTemp.IndexOf(str) + str.Length;
if( index != str.Length – 1)
{
for(int j=index;j<strTemp.Length;j++)
{
if(strTemp[j] != ‘\r’)
this.requestValues[2] += strTemp[j];
else
break;
}
}
FileStream newDoc = new FileStream(newFile,FileMode.Create,FileAccess.Write);
BinaryWriter bw = new BinaryWriter(newDoc);
bw.BaseStream.Seek(0,SeekOrigin.End);
while(br.BaseStream.Position < br.BaseStream.Length – 38)
{
bw.Write(br.ReadByte());
}
br.Close();
bw.Flush();
bw.Close();
%>
</body>
</html>
4 Responses to “How to save the MS Office File to remote server with ASP”
Leave a Reply
You must be logged in to post a comment.
January 8th, 2008 at 7:19 am
incupload.asp
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_office
Class upload_office
dim objForm,objFile,Version
Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=””
else
Form=objForm(strForm)
end if
end function
Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function
Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version=”HTTP Upload”
set objForm=Server.CreateObject(“Scripting.Dictionary”)
set objFile=Server.CreateObject(“Scripting.Dictionary”)
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject(“adodb.stream”)
set Data_office = Server.CreateObject(“adodb.stream”)
Data_office.Type = 1
Data_office.Mode =3
Data_office.Open
Data_office.Write Request.BinaryRead(Request.TotalBytes)
Data_office.Position=0
RequestData =Data_office.Read
iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_office.Position = iFormStart
Data_office.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset =”gb2312″
sInfo = tStream.ReadText
tStream.Close
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,”name=”””,1)+6
iFindEnd = InStr(iFindStart,sInfo,””””,1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
if InStr (45,sInfo,”filename=”””,1) > 0 then
set theFile=new FileInfo
iFindStart = InStr(iFindEnd,sInfo,”filename=”””,1)+10
iFindEnd = InStr(iFindStart,sInfo,””””,1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
iFindStart = InStr(iFindEnd,sInfo,”Content-Type: “,1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_office.Position = iInfoEnd
Data_office.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset =”gb2312″
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&”, “&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=””
set tStream =nothing
End Sub
Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_office.Close
set Data_office =nothing
end if
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
End Class
Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = “”
FilePath = “”
FileSize = 0
FileStart= 0
FormName = “”
FileType = “”
End Sub
Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)=”” or FileStart=0 or FileName=”” or right(fullpath,1)=”/” then exit function
set dr=CreateObject(“Adodb.Stream”)
dr.Mode=3
dr.Type=1
dr.Open
Data_office.position=FileStart
Data_office.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
January 8th, 2008 at 7:20 am
uploadphoto1.asp
<!–#include file=”incupload.asp”–>
<%
set upload=new upload_office
set file=upload.file(“FileData”)
sOriginalFileName = File.FileName
if file.filesize>0 then
formpath=”e:/www/”
filename=makefilename(sOriginalFileName)
file.saveas(formpath&filename)
end if
set file=nothing
set upload=nothing
function makefilename(filename)
dim i
randomize
i=int((100*rnd)+1)
filename=getfilename(filename)&i&”.”&getextendname(filename)
makefilename=filename
end function
function getfilename(filename)
dim filename1
filename1=lcase(filename)
filename1=left(filename1,InStrRev(filename1,”.”)-1)
getfilename=filename1
end function
function getextendname(filename)
dim extname
extname=lcase(filename)
extname=right(extname,3)
extname=right(extname,3-instr(extname,”.”))
getextendname=extname
end function
%>
May 21st, 2008 at 9:14 am
Is this example tested, bescause I can not get it to work?
Where is the variable formpath pointing to, a physical path?
October 17th, 2008 at 9:31 am
The install folder has more examples.