Loading large files GWT client side - gwt

I am new to GWT.
I want to load a large text file (50 MB) from GWT client side and output the file content in a textarea.
I tried Requestbuilder and I passed response.getText() to a string. I am able to do this for a 10-12 MB file but then it just hangs. I think it has something to do with some maximum limit of string. I can not pass the output of response.getText() to a file because then I would not be able to read that file from GWT client side as I'd need bufferreader and all.
I don't know how to make server chunk the file and send one by one responses.
Can anybody please help me with it!

Although the best option will be a server servlet to split the file so as the client could show it paginated, another option is to make the browser natively deal with the big data.
Create an iframe whose source is the url of the file in the server. If the server sends the correct headers (text/plain) the browser will show the content correctly.
Frame f = new Frame("path_to_myfile.txt");
f.setSize("600px", "400px");
RootPanel.get().add(f);

Related

iText form filling missing PDF content

I am running into an odd problem with iText. I have a document with a few fields. On my server, I open the local document, set the fields and send the output of the stamper to the browser.
Works perfectly on my local devel machine.
The pdf generated on the server is missing the PDF contents. I only see the content of the fields I set, the rest is completely blank.
Any tips?
Your application on your local machine respects the bytes of the PDF you're using as a template. Your application on the server doesn't respect those bytes. Maybe you've copied the template using the wrong encoding, making all the binary characters corrupt. Or maybe your application is reading the template using the wrong encoding with the same result.
You can find out by opening your PDF file in a text editor (not inside a PDF viewer). Look for the keyword stream and inspect the bytes that follow this keyword. Do you see the difference? In the PDF produced on your local machine, the bytes look like a normal binary stream. In the PDF produced on your server, the bytes look awkward. For instance: it consists of plenty of question marks.
How to solve: check if the template was copied correctly. If so, check the way you're reading the document. For instance: read the PDF template into a byte array without using iText and write it to a new byte array. Can you reproduce the process of corruption? If so, tweak your application (the one that doesn't involve iText) until you've got the correct encoding.

How can i browse file without uploading in GXT?

i'm beginner with GXT and i'm wondering if there is a way to parse a file and extract some informations without uploading it.
i created a formpanel that contains an uploadFile form but i don't know waht's next, how to get the complete path of the file so i can read/write with java io or how to retrieve the file or is there an alternatif solution, thank you.
Best Regards.
You can do it in some modern browsers using bleeding edge HTML5 apis for which you would need to use GWT JSNI code. There are no api's from GWT team as is.
HTML5 FileReader
FileReader includes four options for reading a file, asynchronously:
FileReader.readAsBinaryString(Blob|File) - The result property will contain the file/blob's data as a binary string.
FileReader.readAsText(Blob|File, opt_encoding) - The result property will contain the file/blob's data as a text string.
FileReader.readAsDataURL(Blob|File) - The result property will contain the file/blob's data encoded as a data URL.
FileReader.readAsArrayBuffer(Blob|File) - The result property will contain the file/blob's data as an ArrayBuffer object.
Example of GWT wrapper over these -
https://github.com/bradrydzewski/gwt-filesystem
You can read about it more from here - How to retrieve file from GWT FileUpload component?
IMHO you cannot read it .
Due to security reasons javascript(gwt) doesn't have access to the system drives files.
http://en.wikipedia.org/wiki/JavaScript#Security
see Opening a file in local file system in javascript
In order to get the file you need to make a server call.
Instead you can do your validation server side and throw proper messages to user.
P.S : i am not considering modern browser concept.What happens if someone opened in other than so called modern browsers?? Will the programm runs same?? Its always better to do server side validation..

How can I display the contents of a textfile on a Flextable in GWT?

How can I display the contents of a textfile (a Notepad) on a Flextable in GWT? I have been trying to use FileInputStream but I get an error that
Plugin failed to Connect at Development mode Server at 127.0.0.9997.
I have tried clearing the cache but it didn't help? Does GWT actually allow reading from a textfile? And how can we print the contents apart from on a FlexTable.
You cannot actually read file in GWT(client side). One option would be allow the user to upload the file, and in the server side you read the file and send the contents to client.
In the client you can use the desired widget to show the content.

Create Byte Array from GWT File upload Input

I have a .NET web service that takes a byte array.
I have a GWT client where I want the user to select a file using the FileUpload control and send it to the web service via HTTP stream.
The file upload control contains a method to get the file path of the selected file. How can I then get that file and convert it to a byte array?
I'm open to suggestions on how to get the file to my web service, not quite sure the Byte array will work...
If you want to convert the file in GWT, meaning in the browser. It's not possible to do in the browser using only JavaScript. FileUpload is a html input type file upload can only send the file to a server as in submit a form. For security reasons browsers can't read files from your file system. (You could use a plugin like a flash plugin to get it to work, although I have no examples at hand).
If you want to send the file content to your webservice, you need to or upload it directly to the webservice or send it to another server, convert it and from that server submit it to your webservice or write or find some (flash) plugin that does it for you.
This link maybe helpful yo you How to convert a byte array to a string, and string to a byte array with GWT
The String(byte[] bytes) constructor and String.getBytes() method are not implemented by GWT JRE emulation String class.
This is not possible purely in Javascript(yet) but could be done with flash or a signed applet. Personally what I would do is create a signed applet that would be somewhere on the page but not visible. When the user selects a file to send to the server you would get the file location from the input and send it to the applet which will load the file and return the data to Javascript as a byte array. If you are flexable with changing the web service to accept multi-part form data then you can do so and just include the file upload field as part of a form and submit the form. Now what you want to do is possible with HTML5 and a demonstration can be seen here, so if you are capable of specifying that the users be using at least a semi-HTML5 compliant browser such as FF3.6 or Chrome 6 you may be in luck.

How to serve .RTFs

I support a web-application that displays reports from a database. Occassionally, a report will contain an attachment (which is typically an image/document which is stored in the database as well).
We serve the attachment via a dynamic .htm resource which streams the attachment from the database, and populates the content-type based on what type of attachment it is (we support PDFs, RTFs, and various image formats)
For RTFs we've come across a problem. It seems a lot of Windows users don't defaultly have an assocation for the 'application/rtf' content-type (they do have an association for the *.rtf file extention). As a result, clicking on the link to the attachment doesn't do anything in Internet Explorer 6.
Returning 'application/msword' as the content-type seems to make the RTF viewable when clicking on the link, but only for people who have MS Office installed (some of the users won't have this installed, and will use alternate RTF readers, like OpenOffice).
This application is accessed publicly, so we don't have control of the user's machine settings.
Has anybody here solved this before? And how? Thanks!
Use application/octet-stream content-type to force download. Once it's downloaded, it should be viewable in whatever is registered to handle .rtf files.
In addition to the Content-Type header, you also need to add the following:
Content-Disposition: attachment; filename=my-document.rtf
Wordpad (which is on pretty much every Windows machine) can view RTF files. Is there an 'application/wordpad' content-type?
Alternatively, given the rarety of RTF files, your best solution might be to use a server-side component to open the RTF file, convert it to some other format (like PDF or straight HTML), and serve that to the requesting client. I don't know what language/platform you're using on the server side, so I don't know what to tell you to use for this.