How to save Excel file to local drive from browser - lotus-notes

I've got an XPage that creates an Excel file using server-side javascript (thank to Russ Maher). I know how to save it to the C: drive if I'm running the XPage locally in a browser, but don't know how to save it to the user's machine when it's running on the server without first saving it to the server. The following code is used to save it from the server's perspective.
var fileOut = new java.io.FileOutputStream(directory+fileName);
xl.write(fileOut);
fileOut.close();
Any ideas how I can direct that to the user's drive?

Instead of writing the Excel workbook to a FileOutputStream, you should write it to a ByteArrayOutputStream:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
xl.write(outputStream);
You probably need to use an XAgent to create the output and then link to the XAgent from your XPage. Maybe this blog entry by Declan Lynch combined with this answer on how to do it in a servlet can guide you in the right direction.

Paul Calhoun sent me some sample code that I massaged to get it to produce the spreadsheet that I wanted. I don't know what it was that he'd done that I hadn't, but, for now, I think this is the heart of the solution, just harnessing an OutputStream instead of either a FileOutputStream or ByteArrayOutputStream.
// The Faces Context global object provides access to the servlet environment via the external content
var extCont = facesContext.getExternalContext();
// The servlet's response object provides control to the response object
var pageResponse = extCont.getResponse();
//Get the output stream to stream binary data
var pageOutput = pageResponse.getOutputStream();
// Set the content type and headers
pageResponse.setContentType("application/x-ms-excel");
pageResponse.setHeader("Cache-Control", "no-cache");
pageResponse.setHeader("Content-Disposition","inline; filename=" + fileName);
//Write the output, flush the buffer and close the stream
wb.write(pageOutput);
pageOutput.flush();
pageOutput.close();
// Terminate the request processing lifecycle.
facesContext.responseComplete();
I will gladly provide help if someone else encounters this issue and, hopefully, by the time someone asks, I will understand more of what was different that worked....

Related

Sending excel data to Tally

I have a macro enabled worksheet which saves the data as XML format.
I have prepared the excel template which saves the out put file in XML format. Then i import the XML file in Tally manually with import data option.
Now i am looking for a solution which would make the process automatic i.e. once i save the file as XML format, immediately the data should get imported in Tally without any manual process.
Hope i explained my query properly.
Thanks in Advance.
Best Regards,
You can use Requests to send the xml to tally. Here's a C# example, you can translate it to your preferred language -
private static string Connect(string host, string request)
{
string response = "";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(host);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = (long)request.Length;
StreamWriter writer = new StreamWriter(httpWebRequest.GetRequestStream());
writer.Write(request);
writer.Close();
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream receiveStream = httpResponse.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
response = reader.ReadToEnd();
reader.Close();
httpResponse.Close();
}
Where host is your tally server ip - usually http://localhost:9000/ and request is your xml - load your xml file as a string.
Ensure your ODBC Server is enabled on Tally. Set your Tally to act as both server and client & choose your port - https://help.tallysolutions.com/article/Tally.ERP9/sync/configuring_client_server.htm
You'll get a response from tally which will have either of the following XML tags -
<CREATED>1</CREATED>
or
<ERROR> // some error </ERROR>
Tally Intergration documentation isn't great but it can still give you an idea - http://mirror.tallysolutions.com/tallyweb/tally/td9/Tally.ERP9_Integration_Capabilities.pdf
Lastly, for automatic upload to tally when the file is created - you can use FileSystemWatcher (C#) or Watchdog library to watch for when the file is created/modified within your specified folder. There is plenty of help in regards to this on stackoverflow.

Store PDF for a limited time on app server and make it available for download

Hei there, I'm using PrimeFaces 5/JSF 2 and tomcat!
Can someone show me or give me an idea on how to store pdfs for a limited time on an application server(I'm using tomcat) and then download it (if that's what the user requests). This functionality relates to invoices so I can't use the dataExporter.
To be more specific, I pretty much implemented this but I don't feel so sure about it. One big question is... where do I store my generated files? I've browsed around and people said that it's not ok to save the files in the webApp or in the tomcat directory. What other solutiuon do I have?
Make use of File#createTempFile() facility. The servletcontainer-managed temporary folder is available as application scoped attribute with ServletContext.TEMPDIR as key.
String tempDir = (String) externalContext.getApplicationMap().get(ServletContext.TEMPDIR);
File tempPdfFile = File.createTempFile("generated-", ".pdf", tempDir);
// Write to it.
Then just pass the autogenerated file name around to the one responsible for serving it. E.g.
String tempPdfFileName = tempPdfFile.getName();
// ...
Finally, once the one responsible for serving it is called with the file name as parameter, for example a simple servlet, then just stream it as follows:
String tempDir = (String) getServletContext().getAttribute(ServletContext.TEMPDIR);
File tempPdfFile = new File(tempDir, tempPdfFileName);
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Length", String.valueOf(tempPdfFile.length()));
response.setHeader("Content-Disposition", "inline; filename=\"generated.pdf\"");
Files.copy(tempPdfFile.toPath(), response.getOutputStream());
See also:
How to save generated file temporarily in servlet based web application
Recommended way to save uploaded files in a servlet application
Your question is vague, but if my understanding is good:
First if you want to store the PDF for a limited time you can create a job that clean you PDFs every day or week or whatever you need.
For the download side, you can use <p:fileDownload> (http://www.primefaces.org/showcase/ui/file/download.xhtml) to download any file from the application server.

How to get the uploaded file path in JSF

I'm importing Excel document for reading and displaying the content in the UI. I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.
I need to know how to get the path of the file uploaded using browse in order to read the content of the excel file.
There's a major thinking mistake here.
Imagine that I am the client who want to upload the file and that you are the server who need to get the file's contents. I give you the path c:\path\to\foo.xls as sole information. How would you as being the server ever get its contents? Do you have an open TCP/IP connection to my local hard disk file system? Really? Is everyone else's local disk file system with all that sensitive information really so easily accessible by Internet?
That isn't how uploading files works. This would only ever work if the server runs at physically the same machine as the client, so that you can just use FileInputStream with that path (which would only occur in local development environment).
You should instead be interested in the sole file contents which the client has sent to you along with the HTTP request body. In HTML terms, you can use <input type="file"> for this. But until the upcoming JSF 2.2 there is no equivalent standard JSF <h:xxx> component for this. You need to look for JSF component libraries offering a component for this. In your case, with RichFaces (as tagged in your question), you can use <rich:fileUpload> for this. It's unclear which RichFaces version you're using, but for RF 3.3.3 you can find a complete example in the 3.3.3 showcase site and for RF 4.0 in the 4.0 showcase site.
Whatever way you choose, you should in your JSF managed bean ultimately end up with a byte[] or an InputStream representing the file contents. Then you've all the freedom to store it wherever you want on the server's local disk file system using for example FileOutputStream. You can even also just feed it directly to whatever Excel API you're using, most of them have just a method taking an InputStream or byte[].
you better take a look at this article. The solution that uses Tomahawk 2.0 ( http://myfaces.apache.org/tomahawk-project/tomahawk20/index.html ) by BalusC is great
JSF 2.0 File upload
Everything you need is there
String fileName = FilenameUtils.getName(uploadedFile.getName());
byte[] bytes = uploadedFile.getBytes();
FileOutputStream outputStream = null;
try {
String filePath = System.getProperty("java.io.tmpdir") + "" + fileName;
outputStream = new FileOutputStream(new File(filePath));
outputStream.write(bytes);
outputStream.close();
readExcelFile(filePath);
} catch (Exception ex) {
}
In the above code Iam uploading the file using tomahawk after uploading storing the file in a temporary location.And from there i will be reading using poi.
public static void readExcelFile(String fileName)
{
try{
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(myFileSystem);
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
for(Row row : sheet) {
//u can read the file contents by iterating.
}
} catch (Exception ex) {
}
}

How do you get the filename from the XPages FileUpload Control

In XPages, in the file upload control, after a user selects a file but before it's saved how can you get the filename? I'm not interested in the path as I believe that's not getable due to security issues but I would like to get the filename and extension if at all possible.
Thanks!
Actually you can get the file and fully manipulate it, read it, do whatever you want with it, its stored in the xsp folder on the server, to which you have read/write access... here is a code snippet that interacts with the file, I usually call from beforeRenderResponse...
var fileData:com.ibm.xsp.http.UploadedFile = facesContext.getExternalContext().getRequest().getParameterMap().get(getClientId('<INSERT ID OF UPLOAD CONTROL HERE (ie. fileUpload1)>'));
if (fileData != null) {
var tempFile:java.io.File = fileData.getServerFile();
// Get the path
var filePath:String = tempFile.getParentFile().getAbsolutePath();
// Get file Name
var fileName:String = tempFile.getParentFile().getName();
// Get the Name of the file as it appeared on the client machine - the name on the server will NOT be the same
var clientFileName:String = fileData.getClientFileName();
}
It sounds like you are referring to needing to get the data via CSJS, which you can do with the following code:
var filename = dojo.byId('#{id:fileUpload1}').value.split('\\').pop();
These links should be able to help you.
http://www.bleedyellow.com/blogs/andyc/entry/intercepting_a_file_upload4?lang=en
http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=en

What can be better way to send WBXML data to web server?

Please could you help me in sending a data from Mobile appication to Web Server in WBXML format ? I have got a bit to convert xml to wbxml but not getting how to send that to web server in efficient way ?
I'm not sure I fully understand your question but here goes...
You need to use a POST HTTP request, and write the WBXML data to the connection objects output stream. Here is a brief example, obviously you'll need more code for it to actually work:
byte[] wbxml = getMyWbxmlData();
HttpConnection conn = (HttpConnection)Connector.open("http://myserver.com/mywbxmlhandler");
conn.setRequestMethod(HttpConnection.POST);
OutputStream output = conn.openOutputStream();
output.write(wbxml);
InputStream input = conn.openInputStream(); // This will flush the outputstream
// Do response processing
That is all assuming your WBXML already includes the preamble, such as version number, public code page identifier etc.

Resources