600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > ajax js图片上传到php Ajax上传并预览图片(附代码)

ajax js图片上传到php Ajax上传并预览图片(附代码)

时间:2023-04-02 18:42:22

相关推荐

ajax js图片上传到php Ajax上传并预览图片(附代码)

这次给大家带来Ajax上传并预览图片(附代码),Ajax上传并预览图片的注意事项有哪些,下面就是实战案例,一起来看一下。

1. 直接上最简单的 一种 ajax 异步上传图片,并预览

html:

图片上传 | cookie

file:

desc:

function upload() {

$.ajaxFileUpload({

url : 'upload.htm',

fileElementId : 'images',

dataType : 'json',

data : {desc : $("#desc").val()},

success : function(data) {

var html = $(".images").html();

html += ''

$(".images").html(html);

}

})

return false;

}

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 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();

}

}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。