Js上传图片并生成缩略图
Js上传图片并显示缩略图的流程为
Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图
本文上传图片所用的Js库是ajaxfileupload,下载地址:http://files.cnblogs.com/files/yujie365/ajaxfileupload.js,Html布局采用bootstrap.
后端处理流程使用Java中的Spring框架进行处理
前面页面处理流程
1,Html页面定义File:

2,选择图片并上传:
*绑定fileimage控件的change事件:$("#fileimage").bind("change", uploadFileImage);
*上传图片:
function uploadFileImage() {
var objUrl = getObjectURL(this.files[0]);
// 上传图片
uploadFile("uploadimage/fileimage", "fileimage", function(result) {
});
}
//uploadUrl:服务器用于接收图片的接口
//fileElementID:file控件的id
//callback:图片上传完成后的回调方法。
function uploadFile(uploadUrl, fileElementID, callback) {
$.ajaxFileUpload({
url : uploadUrl,
secureuri : false,
type : 'post',
fileElementId : fileElementID,
dataType : 'JSON',
success : function(data) {
//data:服务器返回的结果
if (data&& callback != null) {
callback(data);
}
},
error : function(data, status, e) {
}
});
}
*显示缩略图:
更新上面uploadFileImage方法为下面代码:
function uploadFileImage() {
var objUrl = getObjectURL(this.files[0]);
// 上传图片
uploadFile("uploadimage/fileimage", "fileimage", function(result) {
// 显示缩略图
console.log("objUrl = " + objUrl);
if (objUrl) {
$("#fileimage_thumbnail").attr("src", objUrl);
}
});
}
后台Java处理流程
1,后台接收图片的方法如下:
@RequestMapping(value = "uploadimage/fileimage")
public void uploadPicture(HttpServletRequest request) {
try {
String filePath = uploadFile(request, pictureUploadService);
if (!Tools.isEmptyString(filePath)) {
setResult(filePath);
logHelper.info("图片上传成功...");
} else {
setResult(
MessageManager.getInstance().getMessage(
"pictureuploadfailed"),
ResultType.Error);
logHelper.info("图片上传失败...");
}
} catch (IllegalStateException e) {
logHelper.warn("图片保存失败:" + e.getMessage());
} catch (IOException e) {
logHelper.warn("图片保存失败:" + e.getMessage());
}
}
2,存储图片处理流程
private String uploadFile(HttpServletRequest request,IUploadFile uploadFile) throws IllegalStateException,
IOException {
String filePath="";
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Iterator<String> fileNames = multipartHttpServletRequest
.getFileNames();
while (fileNames.hasNext()) {
MultipartFile file = multipartHttpServletRequest
.getFile(fileNames.next().toString());
if (file != null) {
filePath = uploadFile.uploadFile(file);
if (!Tools.isEmptyString(filePath)) {
setResult(filePath);
} else {
setResult(
MessageManager.getInstance().getMessage(
"pictureuploadfailed"),
ResultType.Error);
}
}
}
}
return filePath;
}
public interface IUploadFile {
public String uploadFile(IMultipartFile multipartFile) throws IllegalStateException, IOException ;
}
@Service
public class PictureUploadService extends BaseService implements IUploadFile {
/**
* 上传图片
* @param multipartFile
* 用户所上传的图片文件
* @return
* @throws IllegalStateException
* @throws IOException
*/
@Override
public String uploadFile(MultipartFile multipartFile) throws IllegalStateException, IOException {
String relativePath = Helpers.getImageFolderPath();
String imageSavedPath = IOTools.transferFile(multipartFile,
ConfigHelper.IMAGE_SAVED_PATH, relativePath);
logger.info("图片保存路径:" + imageSavedPath);
return imageSavedPath;
}
}
package com.weijiazhe.utils;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import org.springframework.web.multipart.MultipartFile;
import com.weijiazhe.weijiazheutils.ConfigHelper;
public class IOTools {
/**
* 转移文件
*
* @param multipartFile
* @param fileAbsoluteSavedPath
* @param relativePath
* @return
* @throws IllegalStateException
* @throws IOException
*/
public static String transferFile(MultipartFile multipartFile,
String fileAbsoluteSavedPath, String relativePath)
throws IllegalStateException, IOException {
if (!Tools.isEmptyString(relativePath)) {
String fileName = Tools.getGuid();
String prefix = IOTools.getPrefix(multipartFile
.getOriginalFilename());
relativePath += "\\" + fileName + prefix;
String fileSavedPath = fileAbsoluteSavedPath + relativePath;
File file = new File(fileSavedPath);
//文件不存在则创建文件.
if(!file.exists()){
//String dirName=file.getAbsolutePath();
//if(file.isDirectory()){
file.mkdirs();
//}
//fileSavedPath+="\\" + fileName + prefix;
//file=new File(fileAbsoluteSavedPath);
//file.createNewFile();
}
multipartFile.transferTo(file);
}
return relativePath;
}
}
Js上传图片并生成缩略图的更多相关文章
- C#上传图片和生成缩略图以及图片预览
因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...
- PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图
添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...
- MVC4 上传图片并生成缩略图
Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...
- C#上传图片同时生成缩略图,控制图片上传大小。
#region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...
- thinkphp上传图片,生成缩略图
Image.php <?php /** * 实现图片上传,图片缩小, 增加水印 * 需要定义以下常量 * define('ERR_INVALID_IMAGE', 1); * define('ER ...
- C# webform上传图片并生成缩略图
其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好! void UpImgs() { if (FileUpload1.HasFile) { str ...
- MVC3.0 上传图片并生成缩略图
转自:http://mikelai.blog.163.com/blog/static/18411126620118771732675/ Controller: public ActionResult ...
- 一例tornado框架下处理上传图片并生成缩略图的例子
class coachpic(RequestHandler): @gen.coroutine def post(self): picurl = self.request.files[] print(& ...
- Asp.Net 上传图片并生成高清晰缩略图
在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的.baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码, ...
随机推荐
- atom的react自动补全插件
atom-react-autocomplete–项目内,组件名及状态的自动补全 autocomplete-js-import–模块导入智能提示 emmet-jsx-css-modules– React ...
- 关于python内存管理里的引用计数算法和标记-清楚算法的讨论
先记录于此,后续有时间再深究吧: 1.https://www.zhihu.com/question/33529443 2.http://patshaughnessy.net/2013/10/30/ge ...
- [React] Build a slide deck with mdx-deck using Markdown + React
In this lesson we'll use mdx-deck to create a slide deck using Markdown and React. We'll look at add ...
- label显示不同颜色的字体
NSString *contentSrt = [NSString stringWithFormat:@"%@ (%@)",categoryModel.categoryName, c ...
- OpenStack QA
1 能用devstack部署生产环境的OpenStack吗? 不能,记着,不能!有些选项对生产环境不合适.生产环境能够考虑使用Crowbar部署OpenStack.相关文档例如以下:http://op ...
- libsqlite3.dylib与libsqlite3.0.dylib的差别
在我们加入数据库框架时,在搜索框中输入sqlitekeyword,以下列表区会显示libsqlite3.dylib,libsqlite3.0.dylib. 此时我们选择libsqlite3.0.dyl ...
- my.os.ClickThisWindow.ClickThisPoint.py
my.os.ClickThisWindow.ClickThisPoint.py
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制 如果您使用的电脑要操作的是office2 ...
- [Android6.0][RK3399] 双屏异显代码实现流程分析(二)【转】
本文转载自:http://blog.csdn.net/dearsq/article/details/55050125 Patch Code dtsi rk3399-androiddtsi rk3399 ...
- PL/SQL -->隐式游标(SQL%FOUND)
PL/SQL -->隐式游标(SQL%FOUND) 分类: SQL/PLSQL 基础2010-12-22 16:23 4084人阅读 评论(0) 收藏 举报 sqlexceptionoracle ...