2014-02-17 31 views
0

我在使用「OpenDrive API」上傳文件時遇到問題。我可以連接並做其他事情。 這裏是開發者API:OpenDrive Api如何使用indy在塊中上傳文件?

使用HTTP分析,我可以看到像Createupload,Openupload和Closeupload但我操作觀察,沒有數據包上傳(例如:上傳傳輸5MB)期間傳輸..我不能圖out如何在post後處理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; 

回答

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 
+0

是,Create_Upload_File_Chunk_Post是必需的,但我無法處理這個帖子。所需的參數是「Chunk_Offset」和「Chunk_size」。我如何發佈他們? 任何代碼段或示例將不勝感激。 –

+0

它們以POST數據的形式發送,就像其他字段一樣。你正在創建塊,所以你應該知道你的塊有多大,以及每個塊的文件偏移量是多少。什麼是完全難以理解的?用'TFileStream'打開文件並循環,直到到達EOF。對於每個循環迭代,將'chunk_offset'設置爲當前的'TFileStream.Position',然後從文件中讀取一個數據塊並將'chunk_size'設置爲讀取的字節數,然後對該塊進行POST,然後重複。 –

相關問題