How ajax file uploads work? - java

We know that, JQuery ( or javascript ) can not access local file system. So how does the various ajax file upload plugins of jquery actually work and even show the progress bar ?
I once made an File Uploader using Java Applet and I could do it because somehow I was able to read the file from the local file system using applet. Because I was able to read it, I sent it a 100 lines at a time using ajax.post in multiple parts of 100 lines and as each of the part is successfully sent, manipulated the progress bar accordingly.
But, when we can not read the file from file system, then how do the ajax plugins of jquery show the progress bar and upload the files asynchronously ?

Typically, there is a handler on the server side accepting the post, and then another handler to report progress of any upload.
If you give an example framework to use, we can give a more specific answer.

They all have some sort of back end component that runs on asp/.net or .php (there are others, of course)
There is no way to implement an uploader on your site without something on the back end to save the files.

They use flash. See: http://www.uploadify.com/
Some, now use HTML5 features.

Not necessary that it will use flash . I have custom uploader where I am showing progress in a div with filling it by color based on process done . I have a uploadtracker.js which returns progress done and based on that I saw that div process based on its return value.
Check these samples .
http://malsup.com/jquery/form/#file-upload
Here are some other in case any of them helps .
http://www.tutorialchip.com/jquery/9-powerful-jquery-file-upload-plugins/

Related

Accessing the same file

I have a situation where in I write to a text file programmatically using java and simultaneously I read from the same file using jQuery.
The problem I face is jQuery is unable to find the updated content whenever a content is written into the text file via java.
I have Googled a lot but the only results I find are for java and java processing and not for java and javascript (i.e A Client side and Server side)
I am not sure if this is even possible.
More about the question:
I write into the file the crawling results using java and I am trying to display the same using javascript (jQuery.post() method).
JAVA
A multi-threaded crawling program that crawls a website and does some functionality. I am trying to write some content into a text file using the same java program as and when the crawling happens. The content I write mostly are the details about which thread is getting invoked and what is the current link that is being crawled.
The reason I write this in the text file is I need to show the output in the UI so that people looking at the UI will understand what happens.
Writing happens perfectly as expected.
JAVASCRIPT (jQUERY)
This using the
jQuery.get or post ("sample.txt", function (result) {
$("#someID").html(result);
});
It reads from the text file normally but when java and javascript both are trying to access the file, It is the java that dominates leaving javascript behind thus jQuery is unable to fetch the updated content as and when it happens.
I guess this explanation is more than sufficient to make people understand what exactly my problem is !
On the whole, java and javascript try to access the same file at the same time. So there comes this issue.
Any help is appreciated.
Thanks in advance
I think the file is cached. Easiest thing is to request the file by different urls. Try something like "sample.txt?rnd="+Math.rand()
There can be synchronization problems and your data will be corrupted.
I have a question, is it must be done with Ajax? I think you are trying to figure out about
Ajax push and pull
This is not very easy to do and I wouldn't really recommend it. However, there is a better technology called websocket. So what you can do is, client can submit request to the server to write data into a file then server can send back updated content to the client. Moreover, this is much better than achieving the same objective through numerous amount of HTTP requests.
Additionally, if you want the crossbrowser compatibility, have a look at http://socket.io/
Thanks for all those who were trying to help me out.
I have finally come up with a solution. I, instead of using jquery post to directly read from file, am using another jsp file that reads the file contents and prints using out.println on screen, and after which I am using jQuery post to get the content written by that jsp file. Hence the synchronization problem is avoided.
Here is more about my explanation:
Earlier I had
java program -> Text File <- javascript (jQuery post) // Resulted in synchronization problem where in javascript was not able to access the updated content.
Now
java program -> Text file <- JSP file <- javascript (jQuery post) // Avoided the synchronization problem as that file is accessed by the same server side language. After that jQuery reads the content printed by JSP page.
After many changes, finally came up with one good working solution.
Thanks all.

PHP variables to Java applet?

I have an audio visualizer applet I created with processing and minim using fft to analyze the audio. Now I want to feed in songs to the program dynamically with php as the user clicks on a song. I have read about JavaBridge and have it up and running with Tomcat. Would I need to store the song variable values in an xml file and send it over to the .java file? Is that the right approach? And if so, any idea what this would look like? I'm completely new to java and processing. Any help would be incredibly appreciated!
feed in songs to the program dynamically with php as the user clicks on a song.
Translations presumed for answer:
PHP to HTML.
'clicks on a song' to 'clicks on a link'.
Since the HTML has links to songs, add an onclick() handler and in the called function, use JavaScript to call an applet method to load the required song.
So long as the applet is loading the song from the code base or document base, this should be allowable in a sand-boxed applet. Unfortunately doing so from a method called from JS complicates matters slightly, since the security sand-box is tightened even further.
If that is the case here, it will be necessary to wrap the call in a PrivilegedAction and call it using AccessController.doPrivileged(PrivilegedAction)
The fundamental sequence is as follows:
The user request a .php page in the browser.
The web server lets PHP write the page.
It can write HTML with an applet specified, the applet HTML can be written with parameter with values filled in by PHP.
The client receives an HTML page, which starts the applet.
So a click on the page cannot be handled by PHP, in a simple direct way. Follow #AndrewThompson in this.

Java Web application, how to let a client save a generated .txt file?

I have a JSF Web application, and at some point i present the client a big chunk of information, I want to have a save as link, that allows the client to save this information on his computer as a .txt file.
Information on how to achieve this or a good tutorial would be great.
Does this work for you? You probably would need to set the ContentType to "application/octet-stream", otherwise the client's browser will display your text file instead of offering the option to "Save as".
I believe your best bet may be to have that link actually generate an Ajax call to generate the text file and set it as the src attribute of an iframe on the page. That will trigger (I think) the file download box.

Progress Bar using jQuery with a Grails backend?

I've written an excel upload function that allows a user to upload an excel sheet through a web interface and it will commit it to a table. The problem is that with a table with many records, it takes some time to go through each record and check for changes, so it tends to hang. What would be the best solution for a progress bar type control that I could use to show the users that it is indeed processing the workbook?
Thanks.
Maybe a simple "I'm working" message would do, maybe even with an animated wait spinner.
If you have to do it that way, the Grails jQuery plugin works like it should, and jQuery does include a progress bar which is easy to update as long as you know how far along you are. Far easier though would be to use Bobby's recommendation of a wait gif.
If the process takes a long time, you may consider an asynchronous approach where you allow the user to upload one or more files and then provide them with a widget that shows them the status of all of their file uploads. This increases the responsiveness of the application and provides for a better user experience.
See how GMail does file attachments, or how Flickr does image uploads through the site for some examples.

where to save a jpeg file from an applet

I have a java applet that creates a JPEG file. I want to pass that file to a Javascript where it can display and print it. The only way I can think of doing this is to save the jpeg to a temporary storage area on the user's computer and then pass the path of the file to the javascript which picks it up and displays it. This raises a two questions:
Where should the applet store the file. If you suggest the temporary internet files folder, then how do I find that path to that folder?
Is there a better way to do this? Can I pass the JPEG directly from java to javascript without first writing out to a disk?
Thank you in advance for your help.
To store file on users's machine your applete should be signed, and user should give necessary permissions to your applet (through special dialog window which is shown automatically).
Read this article about modifying DOM from applet
Another approach is to save your image on the server (pass it from your applet to the server) and then reload page (or use Ajax, but in this case you probably have to make ajax calls every few seconds to check if the image is available on the server).
Can't you just have an applet that displays the picture and prints it?
I don't think it'd be possible to do this in IE before IE8 (and it's wimpy even in IE8), but in other browsers your applet could make the image data available to Javascript (please don't say, "a Javascript"; it's like saying, "a FORTRAN" or "a Java") and then from Javascript you could create an <img> tag with a "data URI". See this reference: http://en.wikipedia.org/wiki/Data_URI_scheme

Categories