Here's I want to do, I want to upload a file that will be processed by a servlet. I would use Apache Commons - File Upload to handle the file to be uploaded.
I've seen the gmail-like AJAX file upload, where there would be a hidden iframe that would later be populated with a javascript to stop showing the upload image or displaying a message that the upload is succesful. However, this uses PHP, where the php file to handle the file upload would include the javascript inside the iframe.
My question is, how would I do this in Java using servlets, without resorting to JSP and imitating the above implementation on PHP. I don't even know if this is possible, so please guide me on a good implementation (without external libraries except for commons fileupload).
Note: I am aware that there are libraries out there that could do this easily, but I first want to know how this happens, how this is possible, and to dirty my hands and learn this.
Edit: Just to add, I would use the streaming API of Apache-Commons FileUpload
It is exactly the same.
The client makes an HTTP request to the server (by submitting a form).
The server responds with some HTML (which links to or embeds some JavaScript).
Switching from PHP to Java is just a drop in replacement. You don't need to change any of the JavaScript. The user guide tells you how to set it up.
http://oreilly.com/pub/a/javascript/2002/02/08/iframe.html is the best idea to file-upload. i done file upload using hidden iframe. Please consult with attached link.
Related
It is quite a common question but I can't find an answer to it
I have a simple HTML with an input text box (type=file) and a submit button. On clicking the submit button, I call a js function where I try to get the complete path of the file
var data = $('#fileName').val();
the issue is I am not getting complete file path of the file I am uploading. I know due to security reasons chrome gives me a C:\fakePath\filename and firefox gives me only the fileName. But in case I need a complete path what shall I do?
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
You cannot get the complete path! there is no way to do that!! Even though you are on an intranet and you have enough permissions.
A workaround for this is to have a textarea and ask the user to enter the complete path of the file.
In short you can't have the full name of a file once is loaded on server side, you will just have the file name and its content in a raw byte array (among other attributes). This is not a Java thing nor other server side technologies issue, is related to browser implementation (but it looks that IE6 may contain a flaw about this).
Not directly related to your question but caught my attention
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
Usually, you can't handle a file upload using ajax because it can lead to security holes. Still, there are some browsers (like Chrome and Firefox) that allows you to send a file using XMLHttpRequest but that isn't allowed on some browsers (like IE8-) so you have to use an iframe in order to make the file ajax uploading work.
In order to avoid handling all these problems, I would advice you to use a third-party js library that handles the ajax file upload. An example is blueimp jQuery file upload that also has Java server side examples (DISCLAIMER: I do not work in this project nor I'm associated with blueimp in any way). Note that using this plugin requires that you have a mid knowledge on HTML/JavaScript/jQuery/Java Server Side so if you're a starter it may take you some time to make it work, but once it does is pretty good.
I dont know which technology you are using.. but you can always get file name once it is uploaded on server (Using php or .net )
your steps to upload should be like below:
1) Upload file to the server (e.z. /uploadedFiles/...filename
2) Create a method which will fetch file name from the uploaded path
3) simply insert file name in to the database (this will give you flexibility to change folder name of uploaded docs in future if required)
Generally filenames are not stored as it is . to avoid name conflict in future. So it is a advisable to always rename your filename by adding minutes & seconds after itsname.
If any doubts do ask.
Hope it helps.
Browsers block the filepath access on javascript for securit reasons.
The behavior makes sense, because the server doesn't have to know where the user stores the file on his computer, it is irrelevant to the upload process.
So I'm making a program for android that tries to download something from www.wupload.com. What I want isn't a browser but to interact with the webpage without actually showing it. Like how HtmlUnit is supposed to work.
I'm using apache for the html requests and what I've done so far is send a post that simulates clicking on slow download on the web page. Then I read the response so I can get some variables needed to make the next post and execute the next post. In theory, the web page should be showing the captcha cause the response I get is please enter the captcha, but no image url.
The next step would be to enter the captcha and finally download the file, the problem I'm having is I don't know how to show the captcha image to the user. Do I have to capture it somehow? I know how to make the post to send what the user would type, but the image url of the captcha isn't in the source code.
I thought of inspecting the web page so I could get the url from the DOM tree, like what inspect element on google chrome does, but I have no idea if it's even possible. Any ideas would be great.
thx
The captcha is probably generated using JavaScript. Therefore when you get the source of the website, the captcha hasn't yet been generated and you won't see the image in the source HTML. You would need to run the Javascript somehow. You could try using a WebView because it has built-in support for Javascript, or get a Javascript library for Java and use it somehow. I think it would be a lot of work.
Edit:
Actually, if they are using a thirdy-party captcha library, I'm sure it uses some sort of HTTP request system, so you might be able to inspect it with this plugin for Firefox.
I have often wonder how Facebook is able to handle uploading multiple files on the server when I am uploading my pictures.
I am quite not sure how it is being implemented. As I know, you could only send one file to the server through http one at a time unless you are going to make use of Applets.
Does anybody know how Facebook implements this? Is this Flash or an applet or something?
There's nothing special that you need to do on your web page - multiple <input type="file"> elements in the same <form> will upload multiple files at once.
The tricky part is handling all those files on the server. Take a look at a library such as Apache Commons FileUpload
Edit
You might want to take a look at this thread - people have suggested quite a few readily available components that you can use (note that these are for the client-side i.e. in the browser. You still need to handle the uploaded files on the server using something like the FileUpload library I mentioned before)
I have a requirement to read and write bulk of text files at server side.
How can we write using Ajax, Java... Have little bit idea about these technologies but I just need an idea to implement. After that I feel I can proceed. Could you please help me?
Firstly the text files are going to have to be publicly accessible. They cannot exist in a private context on the server, as JavaScript is a client side technology.
Once they are public, you can simply do an AJAX request on the file location, and then parse them as required.
If having them in a public location is going to be a problem, you will need to have a page that serves them to authenticated accounts only, so only people that are logged in can view them. This means they will have to be served via a server side script at some point, which the AJAX request can then fetch as required.
However if you take this approach, you might as well take out the AJAX and just have the server side script do the processing for you and feed out the result.
I'm building a Flex web application where there is an option to export data from a table to a CSV format (theoreitcally, so far, haha). Since it's a web application, I assumed the best way to go about this would be to send a request to the server to generate the file, and then either send that file (or a link to it) back to the Flex application, and use a FileReference to download the file. Is this the correct way to go about this?
Could anyone give me some pointers on how to do this, however? There are some exisitng remote objects in place that call Java functions on the server, so I thought I'd try something along those lines?
I did a bunch of research, and stumbled across things like HttpResponses and HttpServletResponses in Java, but have no idea how to bridge the gap between the client-side and server-side to use that effectively. Haha.
Thanks!
What I have done is have Flex open a new tab/window and navigate it to a servlet.
navigateToURL(new URLRequest(url),'_blank');
Then use a HttpServletResponse to write the file out to the client, which will then be displayed in the browser. This was for a PDF, though.
In order to use an HttpServletResponse you'll need to write a HttpServlet and configure it in your web.xml. Here is a basic tutorial. Google has a lot more.