`

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

	}
 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics