如何使用indy以块的形式上传文件?

时间:2014-02-17 21:45:55

标签: delphi delphi-xe4 idhttp

我在使用“OpenDrive API”上传文件时遇到问题。我可以连接并做其他事情。 这是开发人员api:OpenDrive Api

使用Http Analyzer我可以看到像Createupload,Openupload和Closeupload这样的操作,但我观察到在上传过程中没有数据包传输(例如:5mb的上传传输)..我无法弄清楚如何在发布时处理chunksize,chunk_offset

以下是我的代码: -

procedure TUploadThread.Execute;
var
 Filename : string;  
 CreateFileParams : TStrings;  
 OpenFileParams :TStrings;
 CloseFileParams : TStrings;
 JResponse : string;
 FileID : string; 
 TempLocation : string;
 JFileCreateObject :ISuperObject;  
 JFileOpenObject : ISuperObject;

begin
  filename := ExtractFileName(frmMain.OpenDialog.FileName);

  try
    CreateFileParams := TStringList.Create;

    CreateFileParams.Add('session_id=' + frmMain.SessionKey);
    CreateFileParams.Add('folder_id=' + frmMain.CurrentFolderID);
    CreateFileParams.Add('file_name=' + FileName);
    CreateFileParams.Add('file_size=' + frmMain.UploadFileSize);
    CreateFileParams.Add('access_folder_id=Public');

    try
      { Try To Create An Instance Of File In The Server }
      JResponse := frmMain.HttpClient.Post('https://dev.opendrive.com/api/v1/upload/create_file.json',CreateFileParams);

      { Now We Have Got FileID & DIR_Update Time from ResponseFileCreate in JSON Format. Lets Parse FileID For later use }
      JFileCreateObject := SO(JResponse);
      FileID := JFileCreateObject['FileId'].AsString; { Here we got FileID}

      OpenFileParams := TStringList.Create;
      OpenFileParams.Add('session_id=' + frmMain.SessionKey);
      OpenFileParams.Add('file_id=' + FileID);
      OpenFileParams.Add('file_size=' + frmMain.UploadFileSize);
      OpenFileParams.Add('access_folder_id=Public');

      try
        Sleep(1000); { Wait For 1 sec Then Perform Another Request }
        JResponse := frmMain.HttpClient.Post('https://dev.opendrive.com/api/v1/upload/open_file_upload.json', OpenFileParams);
        { Now Parse TempLocation From The Json Response In Jresponse }
        JFileOpenObject := SO(JResponse);
        TempLocation := JFileOpenObject['TempLocation'].AsString;

        { Finally, Lets pass the parameters to the close Upload session }
        CloseFileParams := TStringList.Create;
        CloseFileParams.Add('session_id=' + frmMain.SessionKey);
        CloseFileParams.Add('file_id=' + FileID);
        CloseFileParams.Add('temp_location=' + TempLocation);
        CloseFileParams.Add('file_size=' + frmMain.UploadFileSize);
        CloseFileParams.Add('access_folder_id=Public');

         Sleep(1000); { Wait for 1 sec Then Perform Another Request }
         JResponse :=  frmMain.HttpClient.Post('https://dev.opendrive.com/api/v1/upload/close_file_upload.json', CloseFileParams);

        { If Nothing Exception raises, File Is Uploaded..Wait 1 Sec and Reload The Folder }
        frmMain.btnRefresh.Click;

      finally
      end;

    except
      ShowMessage('An Error Occured While Uplaoding File(s).Please Try Aagin');
    end;
  finally
    CreateFileParams.Free;
    OpenFileParams.Free;
    CloseFileParams.Free;
  end;
end;

1 个答案:

答案 0 :(得分:2)

如果我查看API,那么您也应该使用以下两个API。

https://dev.opendrive.com/api/explorer/#!/upload/createResumable_post

https://dev.opendrive.com/api/explorer/#!/upload/createUpload_file_chunk_post

你可以在没有createResumable调用但没有上传文件块的情况下离开。

你的过程应该是这样的:

CreateFile
Open File Upload
Loop
  Upload File Chunk
End Loop
Close File Upload