[Truco Delphi] DropBox API 2 Subir Archivos y Descargar Archivos.
Funciona en Android, Win32, Win64 e iOS.
necesitaras estas unidades.
delphi
Uses IdHTTP, IdSSLOpenSSL, System.JSON;
Funcion de Descargar:
delphi
Function Download(Const Folder, Nam, LocalFile: String): Boolean; Var FIdHTTP: TIdHTTP; file_path: String; StrResp: TmemoryStream; json: Tjsonobject; Begin Result := False; If Folder = '' Then file_path := Nam Else file_path := trim(Folder + '/' + Nam); FIdHTTP := TIdHTTP.Create; StrResp := TmemoryStream.Create; json := Tjsonobject.Create; Try FIdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FIdHTTP); FIdHTTP.IOHandler.LargeStream := True; FIdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + fAccessToken); json.AddPair('path', TJSONString.Create( file_path)); FIdHTTP.Request.CustomHeaders.AddValue('Dropbox-API-Arg', json.ToString); FIdHTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; FIdHTTP.HandleRedirects := true; FIdHTTP.Request.ContentType := ''; Try FIdHTTP.get('https://content.dropboxapi.com/2/files/download', StrResp); Except Result := False; End; Result := FIdHTTP.ResponseCode = 200; StrResp.Position := 0; If Result Then StrResp.SaveToFile(LocalFile); Finally FIdHTTP.Free; StrResp.Free; json.Free; End; End;
Funcion de Subir el Archivo:
delphi
Function Upload(Const Folder, Nam, LocalFile: String): Boolean; Var FIdHTTP: TIdHTTP; file_path, S: String; StrResp: TFileStream; json: Tjsonobject; Begin Result := False; If Not FileExists(LocalFile) Then exit; If Folder = '' Then file_path := Nam Else file_path := trim(Folder + '/' + Nam); FIdHTTP := TIdHTTP.Create; StrResp := TFileStream.Create(LocalFile, fmOpenRead Or fmShareDenyNone); json := Tjsonobject.Create; Try FIdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FIdHTTP); FIdHTTP.IOHandler.LargeStream := True; StrResp.Position := 0; FIdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + fAccessToken); json.AddPair('autorename', TJSOnbool.Create(false)); json.AddPair('path', TJSONString.Create( file_path)); json.AddPair('mute', TJSOnbool.Create(false)); json.AddPair('mode', TJSONString.Create('overwrite')); FIdHTTP.Request.CustomHeaders.AddValue('Dropbox-API-Arg', json.ToString); FIdHTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; FIdHTTP.HandleRedirects := true; FIdHTTP.Request.CharSet := 'utf-8'; FIdHTTP.Request.ContentType := 'text/plain; charset=dropbox-cors-hack'; Try S := FIdHTTP.post('https://content.dropboxapi.com/2/files/upload', StrResp); Except Result := False; End; Result := FIdHTTP.ResponseCode = 200; Finally json.Free; FIdHTTP.Free; StrResp.Free; End; End;
Ejemplo de uso:
delphi
Download('/Apps/inis', 'mail.ini', 'c:\mail.ini'); Upload('/Apps/inis','info.ini','c:\info.ini') ;
Saludos!