Ir al contenido


Foto

[Truco Delphi] DropBox API 2 Subir Archivos y Descargar Archivos.


  • Por favor identifícate para responder
No hay respuestas en este tema

#1 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 mensajes
  • Location127.0.0.1

Escrito 17 agosto 2016 - 05:26

[Truco Delphi] DropBox API 2 Subir Archivos y Descargar Archivos.

 

Funciona en Android, Win32, Win64 e iOS.

 

necesitaras estas unidades.


delphi
  1. Uses
  2. IdHTTP, IdSSLOpenSSL, System.JSON;

Funcion de Descargar:


delphi
  1. Function Download(Const Folder, Nam, LocalFile: String): Boolean;
  2. Var
  3. FIdHTTP: TIdHTTP;
  4. file_path: String;
  5. StrResp: TmemoryStream;
  6. json: Tjsonobject;
  7. Begin
  8. Result := False;
  9.  
  10. If Folder = '' Then
  11. file_path := Nam
  12. Else
  13. file_path := trim(Folder + '/' + Nam);
  14.  
  15. FIdHTTP := TIdHTTP.Create;
  16. StrResp := TmemoryStream.Create;
  17. json := Tjsonobject.Create;
  18. Try
  19. FIdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FIdHTTP);
  20. FIdHTTP.IOHandler.LargeStream := True;
  21. FIdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + fAccessToken);
  22. json.AddPair('path', TJSONString.Create( file_path));
  23. FIdHTTP.Request.CustomHeaders.AddValue('Dropbox-API-Arg', json.ToString);
  24. FIdHTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  25. FIdHTTP.HandleRedirects := true;
  26. FIdHTTP.Request.ContentType := '';
  27. Try
  28. FIdHTTP.get('https://content.dropboxapi.com/2/files/download', StrResp);
  29. Except
  30. Result := False;
  31. End;
  32. Result := FIdHTTP.ResponseCode = 200;
  33. StrResp.Position := 0;
  34. If Result Then
  35. StrResp.SaveToFile(LocalFile);
  36. Finally
  37. FIdHTTP.Free;
  38. StrResp.Free;
  39. json.Free;
  40. End;
  41. End;

Funcion de Subir el Archivo:


delphi
  1. Function Upload(Const Folder, Nam, LocalFile: String): Boolean;
  2. Var
  3. FIdHTTP: TIdHTTP;
  4. file_path, S: String;
  5. StrResp: TFileStream;
  6. json: Tjsonobject;
  7. Begin
  8. Result := False;
  9. If Not FileExists(LocalFile) Then
  10. exit;
  11. If Folder = '' Then
  12. file_path := Nam
  13. Else
  14. file_path := trim(Folder + '/' + Nam);
  15.  
  16. FIdHTTP := TIdHTTP.Create;
  17. StrResp := TFileStream.Create(LocalFile, fmOpenRead Or
  18. fmShareDenyNone);
  19. json := Tjsonobject.Create;
  20. Try
  21. FIdHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FIdHTTP);
  22. FIdHTTP.IOHandler.LargeStream := True;
  23. StrResp.Position := 0;
  24. FIdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + fAccessToken);
  25. json.AddPair('autorename', TJSOnbool.Create(false));
  26. json.AddPair('path', TJSONString.Create( file_path));
  27. json.AddPair('mute', TJSOnbool.Create(false));
  28. json.AddPair('mode', TJSONString.Create('overwrite'));
  29. FIdHTTP.Request.CustomHeaders.AddValue('Dropbox-API-Arg', json.ToString);
  30. FIdHTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  31. FIdHTTP.HandleRedirects := true;
  32. FIdHTTP.Request.CharSet := 'utf-8';
  33.  
  34. FIdHTTP.Request.ContentType := 'text/plain; charset=dropbox-cors-hack';
  35. Try
  36. S := FIdHTTP.post('https://content.dropboxapi.com/2/files/upload', StrResp);
  37. Except
  38. Result := False;
  39. End;
  40. Result := FIdHTTP.ResponseCode = 200;
  41. Finally
  42. json.Free;
  43. FIdHTTP.Free;
  44. StrResp.Free;
  45. End;
  46. End;

Ejemplo de uso:


delphi
  1. Download('/Apps/inis', 'mail.ini', 'c:\mail.ini');
  2.  
  3. Upload('/Apps/inis','info.ini','c:\info.ini') ;

Saludos!


  • 2




IP.Board spam blocked by CleanTalk.