I'm developing a JSP application, and I want to be able to upload the contents of a directory to the server. So the user will select the directory he wants to upload, and somehow all its contents will be uploaded.
Can't be done with regular HTML/Javascript, you have to use either Java or Flash.
There is no support for this in HTML (only uploading of a single file) so there is nothing you can do in your JSP. If you want to do this in java you will have to have something client side, like a signed Applet or a jar that the user downloads and runs.
Looks like it's time for a Framework! Struts2 can handle this type of jobs well and is easy to start with. Here you can take a look at a File Upload sample.
As said, there is no way to do this in JavaScript/HTML.
If you do not want to use a Java Applet (they are clunky and your users may not have Java installed), you can let people upload a zip file and extract it on the server (there is support for this in the Java standard API). Both Windows and Mac OS allow the clients to zip a folder by right-clicking.
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.
Is it possible to upload a file in Play! framework only by giving a path in url?
For example I would like to call:
www.mywebsite.com/upload_PATH
It's for me quite important, because I would like to upload and process a lot of data. Selecting manually 1000 files is too much time consuming and I want to write a program which will make it for me :-) I'm using Play with Java.
If upload_PATH is a local file path on your system, it is not a good way to go.
You should write a Play action where you can upload a file, as it is done in this example.
Then, you should write a HTTP client (started by a main Java method) which go through your files and upload then calling the Play! action. You can use the httpclient Apache library for writing the client part.
As nico_ekito wrote www.mywebsite.com/upload_from_local_path won't work
In case of huge amount of files you can create temporary folder on the distant server and upload your files with FTP. Then in your app you'll need only action for post-upload processing, ie. it can check if file is valid and move it to calculated destination and register in database if required.
Other possibility is using some flash/ajax multi uploader for an example swfupload (don't know it, it's just first hit from the search engine). This approach will be better if you are going give the upload possibility to people who you don't want to give any FTP access.
Finally you can mix the solutions -> use uploader instead of FTP and later post-process new items remotely.
In the java web application need to select the file from server and print to the local printer. how it can be done
Thanks in advance
That's going to be tricky whenever you require a minimum of user interaction (i.e. just click the link and then do the print magic) and it also depends on the type of the file in question. If it is for example a .doc file, then you would basically need to download it to the client environment and open it in the default associated application (MS Word in this case) and then let the application execute the print command. You can't do this from the server side on.
Your best bet is to create an Applet which in turn displays the file tree, downloads the file to the local disk file system on client interaction and makes use of Desktop#print() to print it. E.g.
File file = new File("/temp/file.doc");
// Read file from server using URLConnection, write it to this file and then do:
Desktop.print(file);
But if it are for example plain text files such as text/html, text/xml, etcetera, then you can make use Javascript to load the file into some <div> or <iframe> element and then execute the window.print() method on it, if necessary along with a CSS media rule.
You will need an applet, flash, silverlight, javafx - i.e. an embedded app. There:
download the file from the server by creating a GET request (in an applet - using URL.openConnection()), obtaining the returned bytes and forming an in-memory document
sending that to a printer. If you chose applet - this might help
(I'm not aware whether the same flow can't be achieved with javascript as well)
I am trying use a Java Uploader in a ROR app (for its ease of uploading entire directories). The selected uploader comes with some PHP code that saves the files to the server. I am trying to translate this code to Ruby, but am stumped on this point:
PHP has a very convenient superglobal – $_FILES – that contains a hash of all files uploaded to the current script via the HTTP POST method. It appears Ruby does not have a similar resource. Lacking that, what is the best way to access and save the uploaded files?
I am using the JavaPowUpload uploader ( http://www.element-it.com/OnlineHelpJavaPowUpload/index.html ).
ruby on rails allows you use the application root directory to get at the file stored (wherever you have decided to put it) via #{RAILS_ROOT}.
Check out this tutorial. Not the prettiest method, but it should give you an idea of what needs to be done. Once the file is uploaded, it's just a matter of getting the right path and doing your processing from there.
I need assistance finding a delivery method that best fulfills the following requirements:
We wish to deliver a single file to my clients.
Clients should be able to launch this file from the operating system shell - much like running an '.exe' on Windows.
After being launched, the program/script should be able to display a window with HTML content. this may be done using a stand alone program, a runtime or by running within a browser.
We need the ability to embed a resource within the delivered file, such as an mp3 file, which i can later extract programmatically.
Optimally, the solution should run on Windows, Mac and Linux machines. Less than perfect cross-platform interoperability is acceptable, but we want as broad a penetration as possible.
Clients should not need to pre-install anything (unless it is done transparently), pre-configure anything, or approve any thing for this to happen.
For example:
We could use a regular executable file, written in C++ to do this, but it will not be cross-platform.
We could use a sliverlight XAP file, an adobe Flex file or a Java JAR, but internet explorer warns users when local content is launched. In addition these approaches mean that we have less than perfect penetration, even though it is acceptable in these cases.
We could use a python (or equivalent) script, but the installed-base (penetration) of the python interpreter is not good enough.
Using a standard HTML is not enough because of the difficulty of embedding resources in it. Embedding Silverlight XAML or uuencoded content in HTML causes IE to display a warning.
Using something along the lines of a jpeg as a delivery method is not rich enough since we need to display HTML.
..but internet explorer warns users when local content is launched..
I don't get it, what's the problem with IE saying "Hey this app is trying to run your files!"
I don't mean you don't have a good reason for this, it is just, I don't get it.
IE will only warn the user if the app has not been downloaded and try to access local resources, for instance if running from an applet or a JNLP like this one:(click on the first orange button you see )
But if the users download the jar and run it from the computer ( double click on it ) the app is local and can run without problems.
The jar file is a zip file after all, so you can attach your mp3 file with it. Double click is supported in the desired platform, and the HTML content could be either a local file ( un-packed along with the mp3 file ) or an internet web page.
Java is preinstalled on those OS already.
"internet explorer warns users when local content is launched"
There's a reason for this. How can they distinguish your excellent, well-behaved, polite application from a virus?
Since the line between your app and a virus is very, very blurry, go with any of Silverlight XAP file, an adobe Flex file or a Java JAR.
The IE business is a good thing, not a bad thing.
You could try using the 'Jetty' application server.
This supposes there is a working java environment on the target machine.
Jetty is java servlet container but it is possible to configure
everything (web server, html templates, applications, etc.) in a
single executable jar, which launches the web server and opens a default page.
Exactly how the jar file is launched will vary from platform to platform
but otherwise the user interface will be identicle, and, as its a java application
you can do pretty much anything one it has started.
Sounds like MIME HTML does exactly what you want - unfortunately, it is not supported by many browsers other than IE.
I'd investigate Adobe AIR. It can display both HTML and Flex content in a desktop application without using a web browser. However this will require installation of the AIR runtime, also I'm not sure if the Linux version is out of the beta stage.
http://www.adobe.com/products/air/
You can also use a binary for each platform.
As per your description the app is very simple, and porting from one platf to another sounds like just matter of re-compile and offer binary based on the dist.
Is this an option?