Focal Point
[SOLVED]How to change the suggested name of the file to be saved

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/12510789

April 30, 2009, 06:26 AM
<JJI>
[SOLVED]How to change the suggested name of the file to be saved
Hi,

I have created a flat file that I can successfully hold, with the name and the extension I want, on the reporting server by using a FILEDEF and an ON TABLE SAVE.

EX. FILEDEF TEST DISK D:\IBI\APPS\TESTSITE\test.dta

The problem is that the user needs a mapping to the reporting server to be able to see the file.

As an alternatif I tried: ON TABLE PCHOLD FORMAT ALPHA which prompt the user to save the file. But, the suggested name is something like WFServlet8a6c4dbe (random generated by WF) and no extention. I would like it to be test.dta (name+extention). The name and the extention is important as the file need to be sent to the government

Is this possible?
If not is their a way to save the file directly on the disk of the user by using a dynamic FILEDEF (EX. FILEDEF TEST DISK &Destination).
All suggestions are welcome.

Kind regards,

This message has been edited. Last edited by: <JJI>,
April 30, 2009, 11:34 PM
Waz
As far as I am aware, you cannot name the file returning.

You also cannot have the reporting server hold a file to the clients directory.

If the server has access to a network location that the user has access to, then it could be put there.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

May 01, 2009, 02:15 AM
<JG>
On an IIS based system I use ASP to download the generated file which has to be held in a permanant location on the server.

As you are on Tomcat then you could use JSP to do the same thing.

Here's the ASP method.

The focexec
  
-SET &FILENAME='C:\temp\car.txt';
FILEDEF CARTEXT DISK &FILENAME
-RUN
TABLE FILE CAR
PRINT COUNTRY CAR MODEL
ON TABLE SAVE AS CARTEXT
END
-RUN
-HTMLFORM BEGIN
<HTML>
<head>
</head>
<BODY onLoad="javascript:document.f1.submit();">
<FORM name="f1" METHOD="POST" ACTION="/approot/baseapp/download.asp?file=!IBI.AMP.FILENAME;">
</FORM>
</BODY>
</HTML>
-HTMLFORM END


The ASP code
  
<%@Language="VBScript"%>
<%Option Explicit%>
<%Response.Buffer = True%>
<%
On Error Resume Next
Dim strPath
strPath = CStr(Request.QueryString("file"))
If strPath = "" Then
	Response.Clear
	Response.Write("No file specified.")
	Response.End
ElseIf InStr(strPath, "..") > 0 Then
	Response.Clear
	Response.Write("Illegal folder location.")
	Response.End
ElseIf Len(strPath) > 1024 Then
	Response.Clear
	Response.Write("Folder path too long.")
	Response.End
Else
	Call DownloadFile(strPath)
End If

Private Sub DownloadFile(file)
	'--declare variables
	Dim strAbsFile
	Dim strFileExtension
	Dim objFSO
	Dim objFile
	Dim objStream
	strAbsFile = Server.MapPath(file)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(strAbsFile) Then
		Set objFile = objFSO.GetFile(strAbsFile)
		'-- first clear the response, and then set the appropriate headers
		Response.Clear
		'-- the filename you give it will be the one that is shown
		' to the users by default when they save
		Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
		Response.AddHeader "Content-Length", objFile.Size
		Response.ContentType = "application/octet-stream"
		Set objStream = Server.CreateObject("ADODB.Stream")
		objStream.Open
		'-- set as binary
		objStream.Type = 1
		Response.CharSet = "UTF-8"
		'-- load into the stream the file
		objStream.LoadFromFile(strAbsFile)
		'-- send the stream in the response
		Response.BinaryWrite(objStream.Read)
		objStream.Close
	         Set objStream = Nothing
		Set objFile = Nothing
	Else 'objFSO.FileExists(strAbsFile)
		Response.Clear
		Response.Write("No such file exists.")
		Set objFSO = Nothing
	End If

End Sub
%>


This is set up to handle binary files but you can process ASCII by switching objStream.Type
May 01, 2009, 10:57 AM
CLH
We do something similar however we have the filedef point the file to a network share using the UNC name we then have created a dashboard that points to the share. We do this as we have reports being ran from multiple systems and this allows them to be written to the same place. Not sure if this would work in your situation.


Webfocus 8.0.7 on Windows
May 01, 2009, 04:23 PM
<JG>
Here is a JSP version that does the same thing so it works with Tomcat etc.

The procedure

  
-SET &DOSPATH='c:\temp\';
-SET &JSPPATH=STRREP (&DOSPATH.LENGTH, &DOSPATH, 1, '\', 2,'//', 80, 'A80');
-SET &JSPPATH=TRUNCATE(&JSPPATH);
-SET &FILE='carx';
-SET &FILENAME='&FILE.EVAL.txt';
FILEDEF &FILE.EVAL DISK &DOSPATH.EVAL&FILENAME.EVAL
-RUN
TABLE FILE CAR
PRINT COUNTRY CAR MODEL
ON TABLE SAVE AS &FILE.EVAL
END
-RUN
-HTMLFORM BEGIN
<HTML>
<head>
</head>
<BODY onLoad="javascript:document.f1.submit();">
<FORM name="f1" METHOD="POST" ACTION="/approot/baseapp/download.jsp?file=!IBI.AMP.FILENAME;&|PATH=!IBI.AMP.JSPPATH;">
</FORM>
</BODY>
</HTML>
-HTMLFORM END


The code for download.jsp

  
<%@ page  import="java.io.FileInputStream" %>
<%@ page  import="java.io.BufferedInputStream"  %>
<%@ page  import="java.io.File"  %>
<%@ page import="java.io.IOException" %>

<%   
   String filename=request.getParameter("file");
   String filepath=request.getParameter("PATH");

BufferedInputStream buf=null;
   ServletOutputStream myOut=null;

try{

myOut = response.getOutputStream( );
     File myfile = new File(filepath+filename);
     
     //set response headers
     response.setContentType("text/plain");
      
     response.addHeader(
        "Content-Disposition","attachment; filename="+filename );

     response.setContentLength( (int) myfile.length( ) );
      
     FileInputStream input = new FileInputStream(myfile);
     buf = new BufferedInputStream(input);
     int readBytes = 0;

     //read from the file; write to the ServletOutputStream
     while((readBytes = buf.read( )) != -1)
       myOut.write(readBytes);

} catch (IOException ioe){
     
        throw new ServletException(ioe.getMessage( ));
         
     } finally {
         
     //close the input/output streams
         if (myOut != null)
             myOut.close( );
          if (buf != null)
          buf.close( );
         
     }   
%>


Have fun.

Oh by the way yes it will work on unix based server environments but you may have to use sed to convert
the UNIX line feeds to Windows carriage return/line feeds before the file can be used.

This message has been edited. Last edited by: <JG>,
May 02, 2009, 09:33 AM
<JJI>
Hi guys,

Thanks for your idea's and solutions. I'm going to try the jsp solution on Monday.

Ci@o
May 02, 2009, 11:44 AM
<JG>
Dirk, Just a note on a possible issue, You said that you wanted a file extension of .DTA

I've tested it and there is no problem correctly saving the file.

However by default .DTA does not have a file association in Windows so if you want to be able to open
the file automatically in notepad or another text editor then you will have to manually select the
application to open the file with. Should only need to do it the first time though.
May 08, 2009, 04:43 PM
<JJI>
Hi JG,

I was awear of this issue, but thanks for the reminder. I almost forgot about it. Many thanks for your help. It works perfectly

Kind regards,