How can I upload file in liferay -
upload file in liferay
can 1 me how can upload file in document , media folder using dlfileentry search didn't exact code. how can this. put file controller in jsp file. tries using following code
uploadportletrequest uploadrequest = portalutil.getuploadportletrequest(actionrequest); string sourcefilename = uploadrequest.getfilename("filename"); system.out.println("file name " + sourcefilename); file file = uploadrequest.getfile("filename"); themedisplay themedisplay = (themedisplay) actionrequest.getattribute(webkeys.theme_display); system.out.println("user id " + themedisplay.getuserid() + ": " + themedisplay.getscopegroupid()); long folder_id = 0; long repositoryid = themedisplay.getscopegroupid(); long parentfolderid = dlfolderconstants.default_parent_folder_id; list<folder> lfolder = dlappserviceutil.getfolders(repositoryid, parentfolderid); (folder folder : lfolder) { system.out.println(lfolder); system.out.println(folder.getfolderid()); } //servicecontext servicecontext = servicecontextfactory.getinstance(actionrequest); servicecontext servicecontext = servicecontextfactory.getinstance(fileentry.class.getname(), actionrequest); system.out.println("hello"); long defaultrepoid = dlfolderconstants.getdatarepositoryid(themedisplay.getscopegroupid(),dlfolderconstants.default_parent_folder_id); dlfileentry dlfileentry=dlfileentrylocalserviceutil.addfileentry(themedisplay.getuserid(),themedisplay.getscopegroupid(), defaultrepoid, 12518, sourcefilename, mimetypesutil.getcontenttype(file), "filetitle", "filedesc", "sss",0,null,file,null,file.length(),servicecontext); dlfileentrylocalserviceutil.updatefileentry(themedisplay.getuserid(), dlfileentry.getfileentryid(), sourcefilename, mimetypesutil.getcontenttype(file), "filetitle", "filedesc", "comment", true, dlfileentry.getfileentrytypeid(), null,file, null, file.length(), servicecontext);
but didn't success.
can 1 copy paste code here please?
thanks in advance
in case stumbles across this:
upload_file.jsp:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <portlet:defineobjects /> <portlet:actionurl name='uploadfile' var="uploadfileurl" windowstate="normal" /> <aui:form action="<%= uploadfileurl %>" method="post" name="fm" enctype="multipart/form-data"> <aui:fieldset> <aui:input type="file" name="file-to-upload"/> <aui:button-row> <aui:button type="submit" /> </aui:button-row> </aui:fieldset> </aui:form>
myportlet.java:
public class myportlet extends mvcportlet { //action method public void uploadfile(actionrequest request, actionresponse response) throws exception { uploadportletrequest uploadrequest = portalutil.getuploadportletrequest(request); servicecontext servicecontext = servicecontextfactory.getinstance( myportlet.class.getname(), uploadrequest); this.uploadfileentity(servicecontext, uploadrequest); response.setrenderparameter("mvcpath", "/html/view.jsp"); } // create folder called "a_folder" in documents & media private void uploadfileentity(servicecontext servicecontext, uploadportletrequest request) throws portalexception, systemexception { string filename = ""; string description = "file description"; try{ // servicecontext.scopegroupid groupid of site long repositoryid = dlfolderconstants.getdatarepositoryid( servicecontext.getscopegroupid(), dlfolderconstants.default_parent_folder_id); folder f = dlapplocalserviceutil.getfolder( repositoryid, 0l, "a_folder"); long folderid = f.getfolderid(); file file = request.getfile("file-to-upload"); filename = request.getfilename("file-to-upload"); string mimetype = mimetypesutil.getcontenttype(file); fileentry entry = dlapplocalserviceutil.addfileentry(servicecontext.getuserid(), repositoryid, folderid, filename, mimetype, filename, description, "", file, servicecontext ); }catch(portalexception e){ _log.error("an exception occured uploading file: " + e.getmessage()); throw e; }catch(systemexception e ){ _log.error("an exception occured uploading file: " + e.getmessage()); throw e; } } private static log _log = logfactoryutil.getlog(myportlet.class); }
Comments
Post a Comment