简体   繁体   中英

JSP Javascript upload file

I'm developing an web app with jsp,servlet in server side and javascript in client side. How can ai upload multiple files(images) to the server(and save them to the db)?

Basically, you include one or more <input type='file'> elements in a form, and have the user submit that form. On the server side, you have to handle the posted form, which frankly is non-trivial, you're best off using something that's already been developed like the Apache commons fileupload handler (or other similar helper).

Right now, you can't use Javascript for this, except that you can add more file upload elements to your form dynamically using Javascript if appropriate (eg, in response to the user clicking an "add another file to upload" link or similar).

But the future is coming: As part of the HTML5 effort, there's a File API being defined which will allow Javascript to have client-side access to the specific files the user identifies by selecting them in a file input element (a <input type='file'> ). This opens the door to (for instance) client-side validation of file size limits, file format restrictions, etc. (These can't take place of server-side validation — client-side validation is always a convenience, not a guarantee — but they make for a much better user experience.) It also opens the door to using those files on the client-side without ever having to send them to the server. Very exciting stuff coming down-the-pike.

As an alternative you can also grab Flash+Ajax to upload multiple files more easily/transparently. The jQuery Uploadify plugin is good with this. I've posted a step-by-step guide how to install and use it in a JSP/Servlet webapplication, check the "update" part of this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM