问题
I have a form that has an option for uploading file
<form type="upload" name="myForm" target="rgUsrStory">
<field name="st_title" title="${uiLabelMap.uStoryTitle}"><text/></field>
<field name="upload_file" title="${uiLabelMap.UploadFile}"><file/></field>
<field name="submitButton" title="${uiLabelMap.submit}"><submit/></field>
</form>
request map:
<request-map uri="rgUsrStory">
<security https="true" auth="true"/>
<event type="java" path="org.ofbiz.webapp.control.usrStory" invoke="rgUsrStory" />
<response name="success" type="view" value="main"/>
<response name="error" type="view" value="login"/>
</request-map>
The event function is working properly, but i need to upload the file also to the server and details of that file to the table named as 'documents', but i don't know how to do that, i searched throw the web but only i found using ftl
, also want to control the file type of defined file that i want to display those file types as allowed file in form during adding user story.
For any guide and help thanks.
回答1:
Please have a look at the image upload functionality in the OFBiz content manager.
There's a form
<form name="ImageUpload" target="uploadImage" title="" type="upload" default-map-name="currentValue"
header-row-style="header-row" default-table-style="basic-table">
<field name="dataResourceId" title="${uiLabelMap.ContentDataResourceId}"><display/></field>
<field name="dataResourceTypeId" ><hidden/></field>
<field name="objectInfo" title="${uiLabelMap.ContentUploadedFile}"><display /></field>
<field name="imageData" entity-name="ImageDataResource" title="${uiLabelMap.ContentFile}"><file/></field>
<field name="submitButton" title="${uiLabelMap.CommonUpload}" widget-style="smallSubmit"><submit button-type="button"/></field>
</form>
The corresponding request in the controller.xml
<request-map uri="uploadImage">
<security auth="true" https="true"/>
<event invoke="persistContentAndAssoc" path="" type="service"/>
<response name="success" type="request" value="UploadImage"/>
<response name="error" type="view" value="UploadImage"/>
</request-map>
The service name in services.xml lead you to the service method
<service name="persistContentAndAssoc" engine="java" transaction-timeout="7200"
location="org.ofbiz.content.ContentManagementServices" invoke="persistContentAndAssoc" auth="true">
<description>Create a Content, DataResource and/or ContentAssoc</description>
<permission-service service-name="genericContentPermission" main-action="CREATE"/>
...
</service>
In org.ofbiz.content.ContentManagementServices#persistContentAndAssoc
the uploaded file is read by
ByteBuffer imageDataBytes = (ByteBuffer) context.get("imageData");
(the corresponding form field).
You will find some other functionality like dealing with the mime type there.
来源:https://stackoverflow.com/questions/28713490/file-upload-in-java-ofbiz