文件上传和文件下载
add by: liang 2020-04-24
文件上传
function upload_file_test()
local lineFiles = CVector();--选择多个本机的文件
local sDefaultDir = '';--打开选择文件的窗口,默认打开哪个路径
local sFileType = '*.png *.jpg';--选择的文件类型,多个类型使用空格分隔
ui:getOpenFileNames('标题:请选择要上传的文件', lineFiles, sDefaultDir, sFileType);
if lineFiles:size()<= 0 then
return;--未选择文件
end
local helpFile = CSelectHelp();--help前4列固定, file_name,file_size,file_crc业务使用,上传函数不使用
--上传到服务器那个目录下,从data_busi目录后边开始,上传成功以后此列由目录会变为服务端文件的相对路径
helpFile:addField('remote_path');
helpFile:addField('local_file');--本地文件绝对路径
helpFile:addField('new_file_name');--上传的文件如果需要改文件名,这里是上传后服务器的文件名,为空即不改文件名
helpFile:addField('status');--状态返回,1为上传成功,其它为上传失败
helpFile:addField('file_name');
helpFile:addField('file_size');
helpFile:addField('file_crc');
--文件上传以后在服务端data_busi/dev_test/2020-04-24下边,单个目录下边文件数量不宜过多,请根据文件数量按月还是按天分目录
local sServPath = 'dev_test/'..string.sub(cutil:getSys14Time(), 0, 10);--按月string.sub(cutil:getSys14Time(), 0, 7)
local line = CVector();
for i=0,lineFiles:size()-1 do
line:clear();
line:push_back(sServPath);
line:push_back(lineFiles:get(i));
line:push_back('');
line:push_back('');
line:push_back(cutil:getFileNameFromDir(lineFiles:get(i)));
local iFileSize = cutil:fileSize(lineFiles:get(i));
if iFileSize <= 0 then
return ui:tip('文件'..lineFiles:get(i)..'为空文件,不允许上传');
end
line:push_back(iFileSize);
line:push_back(cutil:getFileCRC(lineFiles:get(i)));
helpFile:addVectorValue(line);
end
helpFile:dump();
if ui:msgConfirm('确认上传文件?') == false then
return;
end
ui:upAndDownFile(true, helpFile);--参数一 true为上传,false为下载
for i=0,helpFile:size()-1 do
if helpFile:vsi(i, 3) ~= '1' then
return;--有文件上传失败了
end
end
local iID = db:getID('test_file', helpFile:size());
--注意:helpFile:vs(i, 'remote_path')上传成功以后此列由目录会变为服务端文件的相对路径,保存数据库,可用户下载
local sql_for = "insert into test_file (file_id, file_path, file_name, file_size, file_crc) values (%d, '%s', '%s', '%s', '%s');"
local sql_all = '';
for i=0,helpFile:size()-1 do
sql_all = sql_all..string.format(sql_for, iID, helpFile:vs(i, 'remote_path'),
helpFile:vs(i, 'file_name'),helpFile:vs(i, 'file_size'),helpFile:vs(i, 'file_crc') )
iID = iID + 1;
end
print( sql_all )
--db:execSQL(sql_all, '')
return ui:tip('全部上传成功');
end
upload_file_test();
文件下载
function download_file_test()
--使用刚才上传成功的文件进行下载
local tmp = [[insert into test_file (file_id, file_path, file_name, file_size, file_crc) values (1000, 'dev_test/2020-04-24/com_arrow.png', 'com_arrow.png', '1165', '2BBD');
insert into test_file (file_id, file_path, file_name, file_size, file_crc) values (1001, 'dev_test/2020-04-24/com_arrow_down.png', 'com_arrow_down.png', '1018', 'A07F');
insert into test_file (file_id, file_path, file_name, file_size, file_crc) values (1002, 'dev_test/2020-04-24/com_arrow_left.png', 'com_arrow_left.png', '1034', '9572');
insert into test_file (file_id, file_path, file_name, file_size, file_crc) values (1003, 'dev_test/2020-04-24/com_arrow_up.png', 'com_arrow_up.png', '1177', '7FD7');]]
local helpFile = CSelectHelp();
helpFile:addField('remote_file');--服务器相对路径文件
helpFile:addField('local_path');--下载到本地哪个目录, 下载完成后会变为文件的绝对路径
helpFile:addField('file_name');--预留列,不使用
helpFile:addField('status');--1为下载成功,其它为失败
local sLocalPath = cutil:getExePath()..'/data_busi';
local line = CVector();
line:push_back('dev_test/2020-04-24/com_arrow.png');
line:push_back(sLocalPath);
line:push_back('');
line:push_back('');
helpFile:addVectorValue(line);
line:clear();
line:push_back('dev_test/2020-04-24/com_arrow_down.png');
line:push_back(sLocalPath);
line:push_back('');
line:push_back('');
helpFile:addVectorValue(line);
ui:upAndDownFile(false, helpFile);
for i=0,helpFile:size()-1 do
print( helpFile:vs(i, 'local_path') )
if helpFile:vsi(i, 3) ~= '1' then
return;--有文件下载失败了
end
end
helpFile:dump();
--以后为打开文件方法,路径必须使用发斜杠
local sLocalFullFile = string.gsub(helpFile:vs(0, 'local_path'), '/', '\\');
print( sLocalFullFile )
os.execute( string.format([[explorer "%s"]], sLocalFullFile) );--调用widows打开文件,类似双击打开文件一样
end
download_file_test();