Uploadify 控件上传图片 + 预览
jquery的Uploadify控件上传图片和预览使用介绍。
在简单的servlet系统中和在SSH框架中,后台处理不同的,在三大框架中图片预览时费了不少力气,所以下面将两种情况都介绍一下。
1,前台代码
script:
- $("#uploadify").uploadify({
- 'langFile' : '<%=request.getContextPath()%>/config/juploadify/uploadifyLang_cn.js',
- 'swf' : '<%=request.getContextPath()%>/config/juploadify/uploadify.swf',
- 'uploader' : '<%=request.getContextPath()%>/UploadAction/uploadImg.action', //图片上传的action方法
- 'queueID' : 'fileQueue',
- 'auto' : true,
- 'multi' : true,
- 'buttonCursor' : 'hand',
- 'fileObjName' : 'uploadify',
- 'buttonText' : '上传图片',
- 'height' : '25',
- 'progressData' : 'percentage',
- 'fileTypeDesc' : '支持格式:jpg/gif/jpeg/png/bmp.',
- 'fileTypeExts': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',//允许的格式
- onUploadSuccess : function(file,data,response) {
- //设置图片预览
- var _arr_val = data.split(",");
- $("#photo").val(_arr_val[1]);
- $("#pic_view").attr("src","<%=request.getContextPath()%>" + _arr_val[1]);
- }
- });
html
- <td width="15%" rowspan="9" align="center">
- <div id="fileQueue" style="width:200px;"></div>
- <input type="file" name="uploadify" id="uploadify" />
- <input type="hidden" name="photo" id="photo" value="">
- <div id="pic_view_div" style="width:150px; height:180px">
- <img src="<%=request.getContextPath()%>/image/nopic.gif" width="150" height="180" id="pic_view">
- </div>
- </td>
2,servlet中后台处理
- public void uploadImg(){
- // 解决服务器端消息返回客户端后中文字符乱码
- response.setHeader("Content-Type", "text/html;charset=UTF-8");
- HttpSession session = request.getSession();
- PrintWriter outs = response.getWriter();
- //获取上传图片的路径
- String savePath = request.getSession().getServletContext().getRealPath("");
- String saveDirectory = savePath + "/Imgupload";
- File file = new File(saveDirectory);
- if (!file.exists()) {
- file.mkdirs();
- }
- //设置图片大小
- if (StringUtils.isBlank(this.fileSizeLimit.toString())) {
- this.fileSizeLimit = "100";// 默认100M
- }
- int maxPostSize = Integer.valueOf(this.fileSizeLimit).intValue() * 1024 * 1024;
- String encoding = "UTF-8";
- MultipartRequest multi = null;
- try {
- multi = new MultipartRequest(request, saveDirectory, maxPostSize, encoding);
- } catch (Exception e) {
- System.out.println("发生异常:" + e.getMessage());
- return;
- }
- //图片预览功能实现
- String _result = "";
- Enumeration files = multi.getFileNames();
- while (files.hasMoreElements()) {
- String name = (String) files.nextElement();
- File f = multi.getFile(name);
- if (f != null) {
- String fileName = multi.getFilesystemName(name);
- String lastFileName = saveDirectory + "/" + fileName;
- result += multi.getOriginalFileName(name) + "," + savePath + "/" + fileName;
- }
- }
- outs.print(_result);
- }
注意:action方法返回的字符串为图片的名称和图片的路径。
3,SSH框架中使用
- package cn.com.zzcy.base.action;
- import java.io.File;
- @Namespace("/UploadAction")
- public class UploadAction {
- private File uploadify;
- public File getUploadify() {
- return uploadify;
- }
- public void setUploadify(File uploadify) {
- this.uploadify = uploadify;
- }
- private String uploadifyFileName;
- public String getUploadifyFileName() {
- return uploadifyFileName;
- }
- public void setUploadifyFileName(String uploadifyFileName) {
- this.uploadifyFileName = uploadifyFileName;
- }
- /**
- * 上传图片
- * @throws IOException
- */
- @Action("uploadImg")
- public void uploadImg(){
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
- String savePath = request.getSession().getServletContext().getRealPath("");
- PrintWriter out = null;
- String resultStr = "";
- String extName = "";// 扩展名
- String newFileName = "";// 新文件名
- try {
- response.setCharacterEncoding("utf-8");
- out = response.getWriter();
- //获取文件的扩展名
- if (uploadifyFileName.lastIndexOf(".") >= 0) {
- extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
- }
- //根据当前时间生成新的文件名称
- String nowTime = new SimpleDateFormat("yyyymmddHHmmss").format(new Date());// 当前时间
- newFileName = nowTime + extName;
- //设置文件保存路径
- String param = request.getParameter("param");
- savePath = savePath + "/ImgUpload/"+param+"/";
- File file = new File(savePath);
- if(!file.exists()){
- file.mkdirs();
- }
- uploadify.renameTo(new File(savePath+newFileName));
- resultStr = uploadifyFileName+","+"/ImgUpload/"+param+"/"+newFileName;
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- out.print(resultStr);
- }
- }
- }
注意:要定义局部变量并提供set方法,这样图片的名称和信息才能自动注入进入。
这里通过request方法获取不到图片信息。。。
4,实现的效果图如下:
原文出自:http://blog.csdn.net/lishuangzhe7047/article/details/41803175
Uploadify 控件上传图片 + 预览的更多相关文章
- jquery实现上传图片预览(需要浏览器支持html5)
jquery实现上传图片预览(需要浏览器支持html5) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- HTML5上传图片预览
<!DOCTYPE html> <html> <head> <title>HTML5上传图片预览</title> <meta http ...
- HTML5上传图片预览功能
HTML5上传图片预览功能 HTML代码如下: <!-- date: 2018-04-27 14:41:35 author: 王召波 descride: HTML5上传图片预览功能 --> ...
- JS 上传图片 + 预览功能(一)
JS 上传图片 + 预览功能 <body> <input type="file" id="fileimg1" style="disp ...
- js兼容火狐显示上传图片预览效果
js兼容火狐显示上传图片预览效果[谷歌也适用] <!doctype html> <html> <head> <meta content="text/ ...
- 通过HTML5 FileReader实现上传图片预览功能
原文:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201706224590.html 在上传图片到服务器之前,我们希望可以预览一下要上传的图片. ...
- 去除ckeditor上传图片预览中的英文字母
去除ckeditor上传图片预览中的英文字母 CKEDITOR.replace('text', { filebrowserImageUploadUrl : 'upload_img.do', langu ...
- HTML5 上传图片预览
html5出现之前如果需要上传图片预览 一般都是先上传到服务器然后远程预览 html5出现之后 有个filereader 解决了这问题 //选中图片之后 $("#fileAddPic&q ...
- [ASP.NET]使用uploadify上传图片,并在uploadify按钮上生成预览图
目录 需求 主要代码 总结 需求 项目中有用到uploadify上传插件,给的原型就是上传成功后替换原来的图片.没办法需求在那儿,也不能修改需求吧,只能想办法解决问题了. 主要代码 修改uploadi ...
随机推荐
- 关于TCP的两个小练习_第一个博客~
先来一个本地的,客户端发送请求,服务端接收请求的简单代码 1 package com.TCP.java; 2 3 import java.io.IOException; 4 import java.i ...
- MySQL主从设定
MySQL的安装 一.下载MySQL http://dev.mysql.com/downloads/mysql/ 二.安装 $tar -xzvf mysql-5.1 ...
- jquery 获取 CheckBox 的状态
<td style="width:220px;vertical-align:central;"><input type="checkbox" ...
- 《Visual C++ 程序设计》读书笔记 ----第8章 指针和引用
1.&取地址:*取内容. 2.指针变量“++”“--”,并不是指针变量的值加1或减1,而是使指针变量指向下一个或者上一个元素. 3.指针运算符*与&的优先级相同,左结合:++,--,* ...
- 支持异步通知的globalfifo平台设备驱动程序及其测试代码
驱动: #include <linux/module.h> #include <linux/types.h> #include <linux/fs.h> #incl ...
- 【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)
C++ Primer Plus 第6版 指针和数组基本等价的原因在于指针算术! 一.指针 ⑴整数变量+1后,其值将增加1: ⑵指针变量+1后,增加的量等于它指向的类型的字节数: ⑶C++将数组名解析为 ...
- 《WPF程序设计指南》读书笔记——第6章 Dock与Grid
1.DockPanel面板 using System; using System.Windows; using System.Windows.Controls; using System.Window ...
- StatusStrip状态栏控件
1.ToolStripStatusLabel statusstrip1.Items[].Text="日期"+DateTime.Now.ToString(); Thread p = ...
- 【iOS】init,loadView,viewDidLoad加载关系
一.loadView 永远不要主动调用这个函数.view controller会在view的property被请求并且当前view值为nil时调用这个函数.如果你手动创建view,你应该重载这个函数. ...
- iOS6 以上设置文本高度,行高(转)
2013-12-09 我来说两句 来源:冻僵的企鹅'zone 收藏 我要投稿 在iOS 7之前,常用下面这个方法计算文本高度sizeWithFont:constrainedToS ...