微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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{
		}

	}
 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐