[opencms-dev] How to let a user upload in VFS

Alessandro Magnolo alessandro.magnolo at gmail.com
Wed Aug 3 17:44:46 CEST 2005


The following is a working solution for OpenCms 6. I'm sorry I don't
have time to explain it now, but I can help if needed.

In my opinion this should be included in a FAQ somewhere in the
official documentation... the question was actually Frequently Asked,
but I couln't find a proper answer.

Regards,
Alessandro Magnolo

-----

String descrizione=null;
String fileName=null;
byte[] fileData=null;

CmsObject cms=CmsFlexController.getCmsObject(request);
List items=org.opencms.util.CmsRequestUtil.readMultipartFileItems(request);
if (items!=null && items.size()>0) {
	Iterator iter = items.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();
		String fieldName = item.getFieldName();
		if (item.isFormField()) {
			if ("descrizione".equals(fieldName)) descrizione=item.getString();
		} else { // !item.isFormField()
			if ("file".equals(fieldName)) {
				fileName=item.getName().replace('\\','/');
				fileName=fileName.substring(fileName.lastIndexOf('/')+1);
				fileData=item.get();
			}
		}
	}
}

if (fileData!=null) {
	cms.getRequestContext().setCurrentProject(cms.readProject("Offline")); 
	String name=cms.getRequestContext().getFolderUri()+"scambio/"+fileName;
	List properties=new ArrayList();
	properties.add(new CmsProperty("Title",descrizione,null));
	if (cms.existsResource(name)) {
		cms.lockResource(name);
		cms.deleteResource(name, CmsResource.DELETE_PRESERVE_SIBLINGS);
	}
	cms.createResource(name, 2, fileData, properties);
	cms.unlockResource(name);
	cms.publishResource(name); 
	cms.getRequestContext().setCurrentProject(cms.readProject("Online")); 
}



More information about the opencms-dev mailing list