delphi 文件上传


前言

delphi的文件上传。上传的目录、文件重复、整个文件上传这些是考虑的因素、及上传逻辑。


一、delphi文件上传

涉及一些文件、流、及基本数据数据的处理。

二、封装的方法

代码如下(示例):

//文件上传
function UploadFileToServer(const BinFolderPath, UserName, DateStr, BizID, FileName: string; const FileData: TBytes;out OriginalFileName:string): Boolean;
var
  PersonFolderPath, DateFolderPath, BizFolderPath, FilePath, NewFilePath, OriginalExt, BaseNameWithoutExt: string;
  DirectoryExists: Boolean;
  FileStream: TFileStream;
  FileCounter: Integer;
begin
  // 构建完整的路径
  PersonFolderPath := TPath.Combine(BinFolderPath, UserName);
  DateFolderPath := TPath.Combine(PersonFolderPath, DateStr);
  BizFolderPath := TPath.Combine(DateFolderPath, BizID);
    // 检查并创建必要的目录
  if not TDirectory.Exists(PersonFolderPath) then
    TDirectory.CreateDirectory(PersonFolderPath);
  if not TDirectory.Exists(DateFolderPath) then
    TDirectory.CreateDirectory(DateFolderPath);
  if not TDirectory.Exists(BizFolderPath) then
    TDirectory.CreateDirectory(BizFolderPath);
  // 使用FileName初始化OriginalFileName,因为FileName应该是原始文件名
  OriginalFileName := FileName;        // OriginalFileName = 'example.txt'
  OriginalExt := TPath.GetExtension(OriginalFileName);   // OriginalExt = '.txt'
  BaseNameWithoutExt := TPath.GetFileNameWithoutExtension(OriginalFileName);  // BaseNameWithoutExt = 'example'

  // 构建文件完整路径
  FilePath := TPath.Combine(BizFolderPath, FileName);

  // 如果文件已存在,则寻找一个唯一的新文件名
  FileCounter := 1;
  while TFile.Exists(FilePath) do
  begin
    // 构建新的文件名,例如“原文件名(1).ext”、“原文件名(2).ext”...
    NewFilePath := TPath.Combine(BizFolderPath, BaseNameWithoutExt + '(' + IntToStr(FileCounter) + ')' + OriginalExt);
    Inc(FileCounter);
    FilePath := NewFilePath;
    OriginalFileName:= BaseNameWithoutExt + '(' + IntToStr(FileCounter) + ')' + OriginalExt;
  end;
  try
    FileStream := TFileStream.Create(FilePath, fmCreate);
    try
      FileStream.Write(FileData, Length(FileData));
    finally
      FileStream.Free;
    end;

    Result := True;
  except

    on E: Exception do
    begin
      //Writeln('Error uploading file: ', E.ClassName, ': ', E.Message);
      Result := False;
    end;
  end;

end;


//调用
procedure TSupQualification_Fr.HandleMultiFileUpload(const Files: TUniFileInfoArray; const BinFolderPath, UserName, DateStr, BizID: string; const ListBox: TuniListBox);
var
  FileName,OriginalFileName: string;
  FileData: TBytes;
  UploadResult: Boolean;
begin
  for var I := Low(Files) to High(Files) do
  begin
    // 获取文件名称
    FileName := ExtractFileName(Files[I].FileName);

    // 获取文件大小并读取文件内容
    SetLength(FileData, Files[I].Stream.Size);
    Files[I].Stream.Position := 0;
    Files[I].Stream.Read(FileData[0], Length(FileData));
     DeleteFile('');
    // 上传文件到服务器并处理结果
    UploadResult := UploadFileToServer(BinFolderPath, UserName, DateStr, BizID, FileName, FileData,OriginalFileName);
    if UploadResult then
      ListBox.Items.Add(OriginalFileName);
  end;

  // 显示成功消息
  if ListBox.Items.Count = Length(Files) then
    ShowOKMsg('上传成功');
end;

总结

提示:1.FileName := ExtractFileName(flbtnautre.FileName[I]); 会报错 range error 2.通过流获取,没有展示真实的文件上传名称
就实现了一个简单的文件上传。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值