开发者

JSP Javascript upload file

I'm developing an web app with jsp,servlet in server side and ja开发者_C百科vascript in client side. How can a i 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 (e.g., 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜