ajax上传图片
2015-09-14 16:06
190 查看
<input type="file" value="上传图片" name="photo" id="uploadPhoto" >
<button class="btn btn-default" type="button" onclick="uploadFile()" value="上传"> 上传</button>
function uploadFile(){
if(!$("#uploadPhoto").val()){
alert("请选择需要上传的图片");
return false;
}
$.ajaxFileUpload ({
url: '../../../loadfile/uploadfile.action', //用于文件上传的服务器端请求地址
secureuri: false, //是否需要安全协议,一般设置为false
fileElementId: 'uploadPhoto', //文件上传域的ID
dataType: 'json', //返回值类型 一般设置为json
//服务器成功响应处理函数
success:function (data, status) {
alert(data);
$("#photonPk").val(data.message);
$("#showDevPic").attr("src","../../../loadfile/showPic.action?sysPk="+data.message)//取图片
},
//服务器响应失败处理函数
error:function (data, status, e){
alert("as:"+e);
}
} );
return false;
}
action中:
private File photo;
private List<String> fileContentType;
private SystemAttachmentService attachmentService;
private Long objectId;
private Long sysPk;
/get set 方法/
@Action("uploadfile")
public String upLoadPic() throws Exception {
SystemAttachment ssa = new SystemAttachment();
byte[] byte_photo = FileCopyUtils.copyToByteArray(getPhoto());
ssa.setContent(byte_photo);
ssa.setCreateTime(new Date());
ssa.setCreateUser(getCurrentUserCode());
ssa.setDeleteFlag(0l);
Long pk = attachmentService.saveSystemAttach(ssa);
getJson().put("message", pk);
return "forjson";
}
@Action("showPic")
public String showViewPic() {
OutputStream out = null;
SystemAttachment ss = null;
if(null!=objectId){
ss = attachmentService.querySysPicForObjectId(objectId);
}else{
ss = attachmentService.querySysPicForSyspk(sysPk);
}
try {
out = getHttpServletResponse().getOutputStream();
if (null != ss) {
try {
out.write(ss.getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
out.write(attachmentService.querySysPicForObjectId(0l).getContent());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
<button class="btn btn-default" type="button" onclick="uploadFile()" value="上传"> 上传</button>
function uploadFile(){
if(!$("#uploadPhoto").val()){
alert("请选择需要上传的图片");
return false;
}
$.ajaxFileUpload ({
url: '../../../loadfile/uploadfile.action', //用于文件上传的服务器端请求地址
secureuri: false, //是否需要安全协议,一般设置为false
fileElementId: 'uploadPhoto', //文件上传域的ID
dataType: 'json', //返回值类型 一般设置为json
//服务器成功响应处理函数
success:function (data, status) {
alert(data);
$("#photonPk").val(data.message);
$("#showDevPic").attr("src","../../../loadfile/showPic.action?sysPk="+data.message)//取图片
},
//服务器响应失败处理函数
error:function (data, status, e){
alert("as:"+e);
}
} );
return false;
}
action中:
private File photo;
private List<String> fileContentType;
private SystemAttachmentService attachmentService;
private Long objectId;
private Long sysPk;
/get set 方法/
@Action("uploadfile")
public String upLoadPic() throws Exception {
SystemAttachment ssa = new SystemAttachment();
byte[] byte_photo = FileCopyUtils.copyToByteArray(getPhoto());
ssa.setContent(byte_photo);
ssa.setCreateTime(new Date());
ssa.setCreateUser(getCurrentUserCode());
ssa.setDeleteFlag(0l);
Long pk = attachmentService.saveSystemAttach(ssa);
getJson().put("message", pk);
return "forjson";
}
@Action("showPic")
public String showViewPic() {
OutputStream out = null;
SystemAttachment ss = null;
if(null!=objectId){
ss = attachmentService.querySysPicForObjectId(objectId);
}else{
ss = attachmentService.querySysPicForSyspk(sysPk);
}
try {
out = getHttpServletResponse().getOutputStream();
if (null != ss) {
try {
out.write(ss.getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
out.write(attachmentService.querySysPicForObjectId(0l).getContent());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
相关文章推荐
- log4j 详解和使用
- 解决:eclipse导入android时工程下没有R文件的问题,以及style.xml文件报错
- 黑马程序员--指针与变量
- android网络框架的封装——volley
- 抓包工具:Fiddler 2-强大效能之二 AutoResponder
- Oracle中varchar ,varchar2的区别
- Matlab处理数据完成后自动发送邮箱提醒
- WPF入门教程系列十八——WPF中的数据绑定(四)
- 关于loadrunner录制不跳转到IE
- IOS不用AutoLayout也能实现自动布局的类(4)----MyTableLayout表格布局
- linear regression example
- spring beans源码解读之 ioc容器之始祖--DefaultListableBeanFactory
- Live555用做RTSPClient时,利用RTP时间戳进行音视频同步的解决方案(必须有RTCP支持才可行)
- 程序员的自我修养——链接,装载与库(第1,2部分)
- SVN分支和标记用法
- 在android中使用POSIX线程
- python编程:使用opencv按一定间隔截取视频帧
- IOS不用AutoLayout也能实现自动布局的类(2)----MyFrameLayout 框架布局
- C# 将RichTextBox中内容的文档以二进制形式存
- oracle存储过程if-else if-else