Ajax upload pictures and preview the simple implementation of _AJAX related

Source: Internet
Author: User

1. Directly on the simplest one Ajax upload pictures asynchronously, and preview

Html:

<! DOCTYPE html>  

Servlet:

protected void DoPost (HttpServletRequest request, httpservletresponse response)
      throws Servletexception, IOException {
    Diskfileitemfactory factory = new Diskfileitemfactory ();
    
    Servletfileupload upload = new Servletfileupload (factory);
    
    String path = Request.getservletcontext (). Getrealpath ("/upload");
    String name = null;
    try {
      list<fileitem> items = upload.parserequest (request);
      for (Fileitem item:items) {
        if (Item.isformfield ()) {
          System.out.println (item.getfieldname () + ":" + Item.getstring ());
        } else {
          name = Item.getname ();
          Item.write (New File (Path,name));
        }
      PrintWriter out = Response.getwriter ();
      Out.print ("{");
      Out.print ("url:\" "+ name +" \ ");
      Out.print ("}");
      
    catch (Exception e) {
      e.printstacktrace ();
    }
  }

2. There will be a ajaxupload.js, there are many online.

The above is a small series for everyone to bring the Ajax upload pictures and preview the full content of the simple implementation, I hope to help you, a lot of support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.