跨平台的文件Path操作
昨天发现服务端上传的文件出问题了,在kbmMW文件服务的root目录下,产生了上万的文件。造成服务不能正常运行,为什么会产生这样的现象呢?
最后发现原来客户在上传文件时有问题,看下面的代码:
复制代码
function RenameFile(aFile:string):string;
var
rp,fn,ext:string;
begin
rp:=System.IOUtils.TPath.GetDirectoryName(aFile);
ext:=ExtractFileExt(fn).Replace('.','');
result:=rp+kbmMWGenerateUniqueFileName('', ext); // 生成唯一文件名
end;
复制代码
aFile参数值可能是这样的:
'2222222\11111\1.jpg'
在Windows下正常,但是在Android下,GetDirectoryName会返回空串,取不到Directory。问题出在“\"上,最后把代码改成这样:
复制代码
function RenameFile(aFile:string):string;
var
rp,fn,ext:string;
begin
fn:=aFile.Replace('\',PathDelim);//将文件名中的路径中的\转换成目标平台的分隔符,保证GetDirectoryName正常工作
rp:=System.IOUtils.TPath.GetDirectoryName(fn).Replace('/','\')+'\';//将文件名中的/转换成Windows平台的分隔符\
ext:=ExtractFileExt(fn).Replace('.','');
result:=rp+kbmMWGenerateUniqueFileName('', ext); // 生成唯一文件名
end;
复制代码
好了,在windows及android平台下都能正常工作了!
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.035058975219727 seconds