attachmentSimple组件删除上传的文件时,默认只是把业务字段中存储的json传中的信息删除 ,而服务器上存储的文件并没有删除
如果要在删除的时候把服务器的上文件也删除需要通过.j或者BIZ层定义action的方法操作服务器
本例是用.j实现的,.j(名字是attachmentSimpleDelete.j)的代码具体如下:
import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.justep.ui.JustepConfig; public class AttachmentSimpleDelete extends com.justep.ui.impl.JProcessorImpl { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String jsonStr = IOUtils.toString(request.getInputStream(), "UTF-8");//获取调用.j时传的postData数据 JSONObject jsonObj = JSON.parseObject(jsonStr); String ownerID = jsonObj.getString("ownerID");//获取postData中的ownerID String storeFileName = jsonObj.getString("storeFileName");//获取postData中的storeFileName //从/UI2/system/service/doc/common/simpleFileStore.j中查看上传的目录 String docStorePath = JustepConfig.getHome() + File.separator + "data" + File.separator + "attachmentSimple"; String filePath = docStorePath + File.separator + ownerID + File.separator + storeFileName;//根据上传的目录和获取的ownerID、storeFileName的值拼接File的路径 File file = new File(filePath); // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 if (file.exists() && file.isFile()) { file.delete(); } else { System.out.println("文件不存在"); } } }
如果是某个功能中的附件组件实现物理删除可以重写图标的点击事件,可以参考http://docs.wex5.com/bex5-ui-question-list-10175/
在自己重写的方法中调用.j,并传删除文件需要的owerID和storeFileName参数,如下:
Model.prototype.romeve = function(owner, event) { var state = owner.$state.get(); var realFileName = event.realFileName.get(); var storeFileName = event.storeFileName.get(); var ownerID = owner.getOwnerID(); // 判断是否是删除 if (state == 'remove') { owner.deleteFile(storeFileName, ownerID); var r = confirm("是否删除服务器上的文件");//confirm提示,选择确定删除服务器上的文件 if (r == true) { var param = { url : require.toUrl('$UI/appdemo/test/process/attachmentSample/attachmentSimpleDelete.j'), postData : { ownerID : ownerID, storeFileName : storeFileName }, callback : function(xhr, ts) { if (biz.Request.isSuccess(xhr)) alert('文件删除成功'); else alert('文件删除失败'); } }; biz.Request.sendHttpRequest(param); } else { return; } } else { owner.downloadFile(realFileName, storeFileName, ownerID); } };
如果是所有功能都需要可以修改组件的js文件/UI2/system/components/justep/attachment/attachmentImageSimple.js中在removeItemByStoreID方法的最后加上.j调用的处理
修改组件的js文件参考http://docs.wex5.com/bex5-ui-question-list-10013/
评一波