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;
是,Create_Upload_File_Chunk_Post是必需的,但我無法處理這個帖子。所需的參數是「Chunk_Offset」和「Chunk_size」。我如何發佈他們? 任何代碼段或示例將不勝感激。 –
它們以POST數據的形式發送,就像其他字段一樣。你正在創建塊,所以你應該知道你的塊有多大,以及每個塊的文件偏移量是多少。什麼是完全難以理解的?用'TFileStream'打開文件並循環,直到到達EOF。對於每個循環迭代,將'chunk_offset'設置爲當前的'TFileStream.Position',然後從文件中讀取一個數據塊並將'chunk_size'設置爲讀取的字節數,然後對該塊進行POST,然後重複。 –