Simple implementation of Ajax upload and preview,

Source: Internet
Author: User

Simple implementation of Ajax upload and preview,

1. upload an image asynchronously and preview the most simple ajax method.

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. Here An ajaxupload. js will be used, which is a lot more online.

The above is all the simple implementation of Ajax Image Upload and preview provided by xiaobian. I hope it will be helpful to you and provide more support for helping customers ~

Related Article

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.