ActiveVFP send a file to the response object

Topics: User Forum
Apr 18 at 10:01 AM
Hello,

I moved from Foxtrails framework to ActiveVFP because some errors with COM. I am trying to send a file, but I am having trouble writing the contents of a file to the response in ActiveVFP.

In Foxtrails I used in default.aspx
response.WriteFile(lcFile)
In ActiveVFP I tried to write in main.prg:
oResponse.ContentType = "application/octet-stream"
oResponse.Write(FILETOSTR(m.lcFile))
It works for a text file, but for a binary file (zip archive) it returns only the first couple of bytes.

I think oResponse does not have the WriteFile from asp.net method because I receive the error
Caught .NET exception, source: <B>MAIN.PRG</B>  00009ag90004  err#= 1426 line=    53 OLE error code 0x80020006: Unknown name.1426
OLE error code 0x80020006: Unknown name.
I also tried with oResponse.BinaryWrite, but on the client I get a corrupt zip file double in size. I checked it with a hex editor and I saw double bytes instead of single bytes.

I use Windows 7 Ultimate 32 bit.

Marius
Coordinator
Apr 23 at 9:44 AM
Edited Apr 23 at 9:46 AM
I did a search of Discussions and found https://activevfp.codeplex.com/discussions/29676 which has oResponse.BinaryWrite near the bottom :
FUNCTION foutPutFileStream
   PARAMETERS lcFile
   IF !FILE(lcFile)
      *oResponse.write("文件不存在")
      RETURN "文件不存在"
   ENDIF   
   LOCAL lcFileName,lcFileSize,lcOldComp,loStream,lcR
   lcFileName=JUSTFNAME(lcFile)
   lcOldComp=SET("Compatible")
   SET COMPATIBLE ON
   lcFileSize=FSIZE(lcFile) 
   SET COMPATIBLE &lcOldComp
   oResponse.Buffer =.t.
   oResponse.clear()
   oResponse.AddHeader("Content-Disposition", strcon("attachment; filename="+lcFileName,9))
   oResponse.AddHeader("Content-Length",lcFileSize)
   oResponse.ContentType ="application/octet-stream"
   oResponse.CharSet="UTF-8"
   loStream=CREATEOBJECT("ADODB.Stream")
   loStream.open()
   loStream.type=1
   loStream.LoadFromFile(strcon(lcFile,9))
   lcR=loStream.Read()
   oResponse.BinaryWrite(lcr)
   loStream.close()
   oResponse.end()
ENDFUNC

If that doesn't work,
Generally, ALL classic ASP methods should work as they do in the original classic ASP. However, if the original classic ASP method is not sub-classed in activevfp.prg (see source code), then you have to refer to the object twice in order to make it work. For example, oResponse.oResponse.BinaryWrite. (if you look at the source you may notice that oRequest has BinaryWrite sub-classed but not oResponse. Therefore, I'm surprised oResponse.BinaryWrite(lcr) worked in the example above))
Apr 23 at 12:01 PM
Thank you very much, Claude! It worked like a charm :)

I think I searched the discussions for "WriteFile", but not for "BinaryWrite". I searched "BinaryWrite" on the net for examples.

I even disabled the handlers in web.config and did some tests with a default.aspx file and I manual called process(), but I end up with other problems.
<% @Page Language="C#" ASPCompat="true" %>
<% @Import Namespace="activevfp_dotnetproxy" %>
<%
server x ;
x = new server() ;
try
   {
    string[] lcFile = x.Process(Request.Url.AbsoluteUri, Request.FilePath) as string[];
    Response.Write(lcFile);
   }

catch(Exception ex)
   {
        Response.Write("Caught XXX .NET exception, source: " + ex.Source + " message: " + ex.Message);  
   }

%>