smarGwt / Grails 上传实例

前台代码 (smarGwt):

 

public DataSourceTextField DataSourceDateFieldUploadingPic(String timeId,String timeName){
		//SC.showConsole();
		//"file"
		new DataSourceValidateFile();
		
		textuploadFile=new  DataSourceTextField(timeId,"<nobr>"+timeName+"</nobr>");
		
		formItem=new CanvasItem(timeId,"<nobr>"+timeName+"</nobr>");
		formItem.setWidth("200");
		
		final DynamicForm picForm=new DynamicForm();
		picForm.setEncoding(Encoding.MULTIPART);
		CanvasItem pictureShowItem=new CanvasItem("pictureShowItem","照片");
		pictureShowItem.setRowSpan(3);
		final Image image=new Image();
		image.setUrl(GWT.getHostPageBaseURL()+"icons/fwsp.png");
		Canvas c=new Canvas();
		c.addChild((image));
		pictureShowItem.setCanvas(c);
		
		UploadItem pictureItem =new UploadItem("file","<nobr>"+timeName+"</nobr>");
		
		SubmitItem submitPic=new SubmitItem("submitPic","上传照片");
		picForm.setItems(pictureShowItem,pictureItem,submitPic);
		picForm.setAction(GWT.getHostPageBaseURL()+"personalInfo/upload");

		
		final Window tipWin=new Window();
		tipWin.setTitle("上传提示窗口");
		HTMLPane htmlPane=new HTMLPane();
		htmlPane.setContents("<IFRAME name=\"uploadFrame2018\" class=gwt-Frame style=\"WIDTH:100%; HEIGHT:100%\" src=\""+GWT.getHostPageBaseURL()+"uploading.gsp\"></IFRAME>");
		htmlPane.setSize("100%", "100%");
		htmlPane.moveTo(0, 20);
		tipWin.addChild(htmlPane);	
	
		tipWin.addCloseClickHandler(new CloseClickHandler() {

			public void onCloseClick(CloseClientEvent event) {
				
	              mainService.getPicName(new AsyncCallback() {
					public void onFailure(Throwable caught) {
						
					}
					@Override
					public void onSuccess(Object result) {
						strValue=result.toString();
						image.setUrl(GWT.getHostPageBaseURL()+"picture/"+result.toString()+"?"+Random.nextDouble());
						tipWin.clear();
					}

				

				});
				
			}
		});
		picForm.setTarget("uploadFrame2018"); 
				submitPic.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						 
						tipWin.setSize("300","300");
						tipWin.setAutoCenter(true);
						tipWin.show();
						tipWin.bringToFront();
						picForm.submitForm();
					}
				})	
				;

		
		picForm.setIsGroup(true);
		picForm.setGroupTitle("上传照片");

		formItem.setCanvas(picForm);
		return textuploadFile;
	}
 

	public DynamicForm createForm(){
		DataSource dataSource=new DataSource();
		dataSource.setClientOnly(true);
		
		DataSourceValidateFile file=new DataSourceValidateFile();
		dataSource.setFields(file.DataSourceDateFieldUploadingPic("attachment1","上传访谈文件"));
		
		final DynamicForm form=new DynamicForm();
		form.setUseAllDataSourceFields(true);
		form.setDataSource(dataSource);
		form.setFields(file.formItem);
		return form;
	}
 

VisitRecordDTO visit=new VisitRecordDTO();
				visit.setAttachment1(form.getValueAsString("attachment1"));
 

 

	public String getPicName(){
		return session["stupic"];	
	}
 

后台代码 (Grails+Groovy):

def upload={
		params.each{
			println it
		}
		
		CommonsMultipartFile uploadFile=params.file
		
	if(!uploadFile.isEmpty()){
		String oFileName=uploadFile.getOriginalFilename()
		Date fname=new Date()
		Long fff=fname.getTime()
		def fileSaveName=fff+oFileName.substring(oFileName.lastIndexOf("."))
		def fileSavePath=request.getRealPath("/")+"uploads\\"+ fileSaveName
		println fileSavePath
		uploadFile.transferTo(new File(fileSavePath) )
		session.setAttribute("stupic",fileSaveName)
		render "上传成功,请关闭提示窗口!"
		}else{
		}

	}
 

如果有不会写的 请给本人留言 谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值