use of org.compiere.util.FileUpload in project adempiere by adempiere.
the class WAttachment method processPost.
// streamAttachment
/**
* Process Post.
* Update / Create Attachment
* Upload Attachment Entry
* @param request request
* @param response response
* @return m_error message
*/
private MAttachment processPost(HttpServletRequest request, HttpServletResponse response, WWindowStatus ws) {
int AD_Attachment_ID = 0;
int AD_Table_ID = 0;
int Record_ID = 0;
String textMsg = null;
FileUpload upload = null;
// URL Encrypted
if (request.getContentType().equals(form.ENC_DEFAULT)) {
AD_Attachment_ID = WebUtil.getParameterAsInt(request, P_Attachment_ID);
AD_Table_ID = WebUtil.getParameterAsInt(request, "AD_Table_ID");
Record_ID = WebUtil.getParameterAsInt(request, "Record_ID");
textMsg = WebUtil.getParameter(request, P_TEXTMSG);
} else {
upload = new FileUpload(request);
m_error = upload.getError();
if (m_error != null) {
log.warning("pocessPost - " + m_error);
return null;
}
AD_Attachment_ID = upload.getParameterAsInt(P_Attachment_ID);
AD_Table_ID = upload.getParameterAsInt("AD_Table_ID");
Record_ID = upload.getParameterAsInt("Record_ID");
}
// WebEnv.dump(request);
log.info("processPost - AD_Attachment_ID=" + AD_Attachment_ID + ", AD_Table_ID=" + AD_Table_ID + ", Record_ID=" + Record_ID + " - Upload=" + upload);
// Check if you own the attachment
if (ws.curTab.getAD_AttachmentID() != AD_Attachment_ID) {
m_error = "Your Attachment not found";
return null;
}
// Check if we can save
if (AD_Attachment_ID != 0 && Record_ID == 0) {
m_error = "Need to save record first";
return null;
}
MAttachment attachment = null;
if (AD_Attachment_ID == 0)
attachment = new MAttachment(ws.ctx, AD_Table_ID, Record_ID, null);
else
attachment = new MAttachment(ws.ctx, AD_Attachment_ID, null);
// Update Attachment Text
if (textMsg != null)
attachment.setTextMsg(textMsg);
// Create Attachment
if (upload != null)
attachment.addEntry(upload.getFileName(), upload.getData());
// Save and update
if (attachment.save())
// update Tab
ws.curTab.loadAttachments();
else {
m_error = "Attachment not saved";
return null;
}
return attachment;
}
use of org.compiere.util.FileUpload in project adempiere by adempiere.
the class RequestServlet method uploadFile.
// doPost
/**
* Upload File
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
private void uploadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileUpload upload = new FileUpload(request);
String error = upload.getError();
if (error != null) {
WebUtil.createForwardPage(response, error, "requests.jsp", 10);
return;
}
Properties ctx = JSPEnv.getCtx(request);
// Get Request:
int R_Request_ID = upload.getParameterAsInt("R_Request_ID");
MRequest req = null;
if (R_Request_ID != 0)
req = new MRequest(ctx, R_Request_ID, null);
if (R_Request_ID == 0 || req == null || req.get_ID() != R_Request_ID) {
WebUtil.createForwardPage(response, "Request not found", "requests.jsp", 10);
return;
}
if (!req.isWebCanUpdate()) {
WebUtil.createForwardPage(response, "Request cannot be updated", "requests.jsp", 10);
return;
}
String fileName = upload.getFileName();
log.fine("R_Request_ID=" + R_Request_ID + " - " + fileName);
// Add Attachment
MAttachment attachment = req.createAttachment();
attachment.addEntry(fileName, upload.getData());
if (attachment.save()) {
String msg = Msg.parseTranslation(ctx, "@Added@: @AD_Attachment_ID@ " + fileName);
req.webUpdate(msg);
req.saveEx();
WebUtil.createForwardPage(response, msg, "requests.jsp", 10);
} else
WebUtil.createForwardPage(response, "File Upload Error - Please try again", "requests.jsp", 10);
log.fine(attachment.toString());
}
Aggregations