Delphi开发中ERP系统实现文件的上传下载查看删除功能
1、首先,需要数据控件做基本的配置
数据控件:
a.配置好数据控件的SQLupdate属性:
b.书写更新操作
UPDATE SRE_MSTR SET SRE_NBR = :SRE_NBR, SRE_DATE = :SRE_DATE, SRE_VEND = :SRE_VEND, SRE_TYPE = :SRE_TYPE, SRE_RMKS = :SRE_RMKS, SRE_FILENAME = :SRE_FILENAME, SRE_FILE_TYPE = :SRE_FILE_TYPE, SRE_REALNAME = :SRE_REALNAME, SRE_AUD_DESC = :SRE_AUD_DESC, SRE_PUR = :SRE_PUR, SRE_PUR_BY = :SRE_PUR_BY, SRE_PUR_DATE = :SRE_PUR_DATE, SRE_QC = :SRE_QC, SRE_QC_BY = :SRE_QC_BY, SRE_QC_DATE = :SRE_QC_DATE, SRE_ENG = :SRE_ENG, SRE_ENG_BY = :SRE_ENG_BY, SRE_ENG_DATE = :SRE_ENG_DATE, SRE_COM_ASS = :SRE_COM_ASS, SRE_PUR1 = :SRE_PUR1, SRE_PUR1_BY = :SRE_PUR1_BY, SRE_PUR1_DATE = :SRE_PUR1_DATE, SRE_QC1 = :SRE_QC1, SRE_QC1_BY = :SRE_QC1_BY, SRE_QC1_DATE = :SRE_QC1_DATE, SRE_ENG1 = :SRE_ENG1, SRE_ENG1_BY = :SRE_ENG1_BY, SRE_ENG1_DATE = :SRE_ENG1_DATE, SRE_SUP_SCO = :SRE_SUP_SCO, SRE_SUP_TYPE = :SRE_SUP_TYPE, SRE_CON = :SRE_CON, SRE_APRV = :SRE_APRV, SRE_APRV_BY = :SRE_APRV_BY, SRE_APRV_DATE = :SRE_APRV_DATE, SRE_VAL = :SRE_VAL, SRE_INVAL = :SRE_INVAL, SRE_INVAL_BY = :SRE_INVAL_BY, SRE_INVAL_DATE = :SRE_INVAL_DATE, SRE_SITE = :SRE_SITE, SRE_WF_STATUS = :SRE_WF_STATUS, SRE_PROG_CODE = :SRE_PROG_CODE, SRE_DOC_CODE = :SRE_DOC_CODE, SRE_CRT_BY = :SRE_CRT_BY, SRE_CRT_DATE = :SRE_CRT_DATE, SRE_MOD_TIMES = :SRE_MOD_TIMES, SRE_MOD_BY = :SRE_MOD_BY, SRE_MOD_DATE = :SRE_MOD_DATE, SRE_PST = :SRE_PST, SRE_PST_BY = :SRE_PST_BY, SRE_PST_DATE = :SRE_PST_DATE, SRE_CHAR1 = :SRE_CHAR1, SRE_CHAR2 = :SRE_CHAR2, SRE_CHAR3 = :SRE_CHAR3, SRE_CHAR4 = :SRE_CHAR4, SRE_CHAR5 = :SRE_CHAR5, SRE_CHAR6 = :SRE_CHAR6, SRE_QTY1 = :SRE_QTY1, SRE_QTY2 = :SRE_QTY2, SRE_FILE = EMPTY_BLOB(),SRE_FILE1 = EMPTY_BLOB(),SRE_FILENAME1=:SRE_FILENAME1 WHERE SRE_NBR = :Old_SRE_NBR RETURNING SRE_FILE,SRE_FILE1 INTO :SRE_FILE,:SRE_FILE1;
c.若采用流数据字段BLOB/CLOB来存储文件,则还应设置字段属性,如下:
至此,配置结束。
2、实现ERP系统中文件上传
procedure Tfrm_SLSUMT01.btn_UploadClick(Sender: TObject); begin inherited; if sre_mstr.IsEmpty then exit; SaveRecord; sre_mstr.Edit; // sre_mstr.Cancel; UploadFileToDB(sre_mstr, 'sre_filename', 'sre_file'); end; procedure Tfrm_SLSFMT01.UploadFileToDB(qry: TyyQuery; FieldName1, FieldName2: String); var FileName: String; begin //保存文件到数据库 if (not qry.Active) then exit; if qry = sfsd4_det then begin if not dlg_Open4.Execute then exit; FileName := dlg_Open4.FileName; try BeginWait; qry.Append; qry.F(FieldName1).AsString := ExtractFileName(FileName); TBlobField(qry.F(FieldName2)).LoadFromFile(FileName); qry.Post; finally StopWait; end; end; if qry = sfs_mstr then begin if not dlg_Open.Execute then exit; FileName := dlg_Open.FileName; try BeginWait; qry.Edit; qry.F(FieldName1).AsString := ExtractFileName(FileName); TBlobField(qry.F(FieldName2)).LoadFromFile(FileName); qry.Post; finally StopWait; end; end; end;
3、实现ERP系统中文件下载
procedure Tfrm_SLSUMT01.btn_downloadClick(Sender: TObject); var filename :String; begin inherited; if sre_mstr.F('sre_filename').AsString = '' then exit; if Ask(priMsg.GetMsg('041')) = ID_NO then exit; //041=确定要下载当前文件吗? //文件名 dlg_download.FileName := sre_mstr.F('sre_nbr').AsString + '_' + sre_mstr.F('sre_filename').AsString ; if dlg_download.Execute then begin filename := dlg_download.FileName; TBlobField(sre_mstr.F('sre_file')).SaveToFile(FileName); with Query do begin try if not DTM.Conn.InTransaction then DTM.Conn.StartTransaction; ExecSQL; DTM.Conn.Commit; except if DTM.Conn.InTransaction then DTM.Conn.Rollback; Raise; end; end; ShowMsg(Format(priMsg.GetMsg('042'), [filename])); //042=文件下载完成! (%s) end; end;
4、实现ERP系统中文件查看
procedure Tfrm_SLSUMT01.btn_viewClick(Sender: TObject); begin inherited; //查看文件 if (sre_mstr.F('sre_filename').AsString = '') then exit; OpenFile(sre_mstr, 'sre_filename', 'sre_file') end; procedure Tfrm_SLSUMT01.OpenFile(qry: TyyQuery; FieldName1, FieldName2: String); var FileName: String; path: array [0..MAX_PATH] of Char; begin //查看文件 if qry.Active and (not qry.IsEmpty) then begin GetTempPath(MAX_PATH, path); TEMP_FILE_PATH := path; if TEMP_FILE_PATH = '' then TEMP_FILE_PATH := '.'; FileName := TEMP_FILE_PATH + '\' + qry.F(FieldName1).AsString; TBlobField(qry.F(FieldName2)).SaveToFile(FileName); if FileExists(FileName) then ShellExecute(Application.Handle, PChar('Open'), PChar(FileName), nil, nil, SW_SHOW); end; end;
5、实现ERP系统中文件删除功能
procedure Tfrm_SLSUMT01.btn_delClick(Sender: TObject); begin inherited; //删除该项 SaveRecord; if sre_mstr.Active and (not sre_mstr.IsEmpty) then if Ask(priMsg.GetMsg('040')) = ID_YES then //040=确认要删除该文件吗? sre_mstr.Edit; sre_mstr.F('sre_filename').AsString := ''; sre_mstr.F('sre_file').AsString := ''; sre_mstr.Post; priMsg.ShowMsg('043'); //043=删除完成! end;
与你共亲到无可亲密时,便知友谊万岁是尽头。