How to stream file from xPages? - xpages

I'm trying to stream the file from file system to browser and cant get it to work properly. I have a xpage with rendered=false and on afterRenderResponse I have the following code:
XspHttpServletResponse response = (XspHttpServletResponse) getFacesContext().getExternalContext().getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=demofile.exe");
File file = new File("path to file");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
etc. .....
Now when I try to open xpage I'm getting the error message as this on console:
java.lang.IllegalStateException: Can't get an OutputStream while a Writer is already in use
at com.ibm.xsp.webapp.XspHttpServletResponse.getOutputStream(XspHttpServletResponse.java:548)
The method 'response.getOutputStream()' raises such error so I cant get output stream to work. Is there anyone having experience with this? I'm simply trying to implement the download service so I can stream files from server file system back to browser.

You can call the facesContext.getOutputStream() in the beforeRenderResponse and NOT from afterRenderResponse.
Refer to the below links for more help:
http://www.wissel.net/blog/d6plinks/SHWL-8BYMW8
http://www.wissel.net/blog/d6plinks/shwl-7mgfbn

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.

Node.js - How to pass a stream from request to xml-stream

I'm trying to download a large XML file and parse it using xml-stream library. I'm using request to download the file and it's capable of streaming the content of the file. Ideally, I'd like to pass that stream directly to xml-stream and have it parsed. But I can't figure out how to connect those two.
Here is the code a have so far:
request('http://example.com/data.xml').pipe(fs.createWriteStream('data.xml'));
...
var stream = fs.createReadStream('data.xml');
var xml = new XmlStream(stream);
Is it possible to connect them directly without a temp data.xml file?
request() returns a Readable stream, so just pass that return value to XmlStream():
var xml = new XmlStream(request('http://example.com/data.xml'));

Epplus xlsx file won't open in libreoffice after downloading, but will open in excel

I'm generating an EEPlus .xlsx file that the user can download after clicking a button. It won't open on my Win8 machine with LibreOffice 4.4.1.2, but it will open on my coworkers Win8 machine using Excel 2013.
I get an error saying the file is corrupt and needs to be repaired, and when I try to repair it, it says it's unable to. My coworker also gets this error but is able to repair it and view the file.
Here's the relevant code for the creation of the downloadable version:
MemoryStream ms = DataTableToExcelXlsx(dt, "Report");
ms.Position = 0;
ms.WriteTo(Response.OutputStream);
Response.ContentType="application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=DataTable.xlsx");
Response.StatusCode = 200;
Now the weird part is that I'm also generating an xlsx file that is attached to an email, and it opens just fine. Neither me nor my coworker receive the error when opening the file attached to to the email.
Relevant code for the creation of the email attached version:
MemoryStream ms = DataTableToExcelXlsx(dtReportData, sReportTitle);
ms.Position = 0;
MailMessage mail = new MailMessage("you#yourcompany.com", sReportEmails);
using (SmtpClient client = new SmtpClient())
{
mail.Subject = "Mixer - Test Scheduler Task Execute.";
mail.Body = string.Format("The following report is executing. ReportSettings Id: {0}", reportSettingsId);
mail.Attachments.Add(new Attachment(ms, sReportTitle + ".xlsx", "application/vnd.ms-excel"));
client.Send(mail);
}
I've also tried using the vnd.ms-excel MIME type for the downloadable version and that didn't work, so I'm at a loss as to why this is happening.
I would have liked to answer this as a comment, but no reputation yet, or whatsoever.
Firstly, Why dont you try to use this content type?
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Currently, it does seem to be wrong. Please see as well this post, in which correct mime types are defined.
Try making a unit test or command line app that creates the xlsx to your local hard drive (avoiding all the the web Response stuff). Then see if the error shows up in that xlsx.
If it does then it is the code that create the file. Post it up and we can take a look.
If it does not then something is up with the response settings. Maybe in the disposition? Here I have an Response stream that I know works: Open ExcelPackage Object with Excel application without saving it on local file path

How to save Excel file to local drive from browser

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....

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