自动上传文件时出现间歇性问题

时间:2020-09-17 01:07:07

标签: file delphi upload

我已经用Delphi / Pascal编写了一些代码来将文件上传到网站。为此,请在单击“上传”按钮之前立即在线程中运行以下代码。该线程查找Windows上载文件对话框并填写。

大多数时候都可以使用。偶尔但以某种频率,它不起作用。发生的事情是我在上载对话框中输入了完整的文件名和路径。由于某些原因,有时Windows将从文件名中删除路径。由于给定的文件名(无路径)在Windows当前所在的目录中不存在,因此上传失败。

例如,我正在上载“ c:\ uploads10 \ 123.pdf”,Windows会(有时)将文件名截断为“ 123.pdf”,该名称在上载对话框默认设置的任何目录中均不存在,因此上载失败。

这让我发疯了一段时间。有人可以告诉我如何解决吗?

  for Looper := 1 to 2 do
  begin
    UploadDialogHandle := FindWindow('#32770', 'Choose File to Upload');
    if UploadDialogHandle = 0 then
      Sleep(100)
    else
    begin
      SetFileUploadDialogPosition(UploadDialogHandle);
      break;
    end;
  end;
  if UploadDialogHandle = 0 then
    exit;

  for Looper := 1 to 1 do
  begin
    // Find the child ComboBoxEx32 window
    UploadDialogFileEditHandle := FindWindowEx(FileUploadDialogHandle, 0,
      'ComboBoxEx32', '');
    if UploadDialogFileEditHandle = 0 then
      Sleep(100)
    else
      break;
  end;

  if UploadDialogFileEditHandle = 0 then
    exit;

  for Looper := 1 to 1 do
  begin
    // Find the child ComboBox window
    UploadDialogFileEditHandle := FindWindowEx(UploadDialogFileEditHandle, 0,
      'ComboBox', '');
    if UploadDialogFileEditHandle = 0 then
      Sleep(100)
    else
      break;
  end;
  for Looper := 1 to 1 do
  begin
    // Find the child Edit window
    UploadDialogFileEditHandle := FindWindowEx(UploadDialogFileEditHandle, 0,
      'Edit', '');
    if UploadDialogFileEditHandle = 0 then
      Sleep(100)
    else
      break;
  end;

  if UploadDialogFileEditHandle = 0 then
    exit;

  Result := False;
  if (UploadDialogHandle = 0) or (UploadDialogFileEditHandle = 0) or
    (FileUploadButtonHandle = 0) then
    exit;
  StrPCopy(aUploadFileName, FileNameString);
  SendMessage(UploadDialogFileEditHandle, WM_SETTEXT, SizeOf(aUploadFileName),
    Integer(@aUploadFileName));
  Sleep(200);
  SendMessage(UploadDialogFileEditHandle, WM_GETTEXT, SizeOf(FileEditContents),
    Integer(@FileEditContents));

  if (StrComp(aUploadFileName, FileEditContents) = 0) then
    SendMessage(FileUploadButtonHandle, BM_CLICK, 0, 0);

0 个答案:

没有答案