Recent

Author Topic: Downloand from dropbox  (Read 124 times)

magleft

  • Jr. Member
  • **
  • Posts: 61
Downloand from dropbox
« on: May 16, 2022, 12:30:39 pm »
Hello.
I would like to create a small application that download and upload a file to dropbox.
Through windows this was done relatively easily through THTTPSend. There is of course the key issue
which gives dropbox, but I think I will be able to solve it.
When I tried to do it through android using the jHttpClient class, when I run it in
in the emulator during downloand does not output any error message, but does not download the file In upload
issues the code 201 but does not upload the file ..
When I try to run it on an android device the application closes.
I tried with jDownloadManager1 but could not see where the headings are defined.
I tried with the THTTPSend class but both in the download and in the upload it returns the code 500.
Does anyone have any idea how the problem could be solved?

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton3Click(Sender: TObject); // downloand file
  2. const
  3.   file_source='"/myfolder/myfile.jpg"';
  4.   downloandfile='newfile.jpg';
  5. var
  6.   http: THTTPSend;
  7.   Url: string;
  8.   jpath:String;
  9. begin
  10.   http := THTTPSend.Create;
  11.   http.Headers.Add ('Authorization:'+atoken) ;  //Authorization
  12.   http.Headers.Add ('Dropbox-API-Arg:'+'{"path":'+file_source+'}') ;
  13.   Url := aurldown;
  14.   jpath :=self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+downloandfile  ;
  15.   if http.HTTPMethod('GET', Url) then
  16.   begin
  17.      HTTP.Document.SaveToFile(afile)  ;
  18.   end
  19.   else
  20.     begin
  21.       ShowMessage('error(' + IntToStr(http.ResultCode) + ')' + http.ResultString);
  22.     end;
  23.   http.Free;
  24.   SHOWMESSAGE('fINISH');
  25. end;
  26.  
  27. //----------------------
  28.  
  29. procedure TAndroidModule1.jButton4Click(Sender: TObject); //uploand file
  30. const
  31.   alocalfile='myfile.jpg';
  32.   file_source='"/myfolder/myfile.jpg"';
  33. var
  34.   http: THTTPSend;
  35.   Url: string;
  36. //  data:TfileStream ;
  37.   data:TStringList   ;
  38.   jpath:String;
  39. begin
  40.   jpath:=alocalfile;   
  41.   http := THTTPSend.Create;
  42.   data:=TStringList .Create();
  43.   data.LoadFromFile(jPath);
  44.   HTTP.MimeType := 'Application/octet-stream';
  45.   http.UserAgent :='api-explorer-client';
  46.  
  47.   HTTP.Headers.Add('Authorization' +':'+atoken) ;
  48.   http.Headers.Add('Dropbox-API-Arg'+':'+' {"path":'+file_source,"mode":{".tag":"overwrite"},"autorename":false,"mute":false,"strict_conflict":false}') ;
  49.  Url := 'https://content.dropboxapi.com/2/files/upload/'  ;
  50.   try
  51.     HTTP.Document.LoadFromFile(jpath);
  52.     if HTTP.HTTPMethod('POST', URL) then
  53.        jPanel1 .Text :='Ολοκληρώθηκε η αποστολή του αρχείου'
  54.        else jPanel1 .Text :='Σφάλμα!!! Δεν έγινε η αποστολή του αρχείου....';
  55.        SHOWMESSAGE(INTTOSTR(HTTP.ResultCode)+':'+HTTP.ResultString);
  56.   finally
  57.     begin
  58.     jPanel1  .Text :=http.ResultString+http.Headers.Text ;
  59.     HTTP.Free;
  60.     end;
  61.     end;
  62.   data.Free ;
  63.   SHOWMESSAGE('OK');
  64. end;
  65. //------------ android  
  66.  
  67. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  68. const
  69.   alocalfile='myfile.jpg';
  70.   file_source='"/myfolder/myfile.jpg"';
  71. var
  72.   Url: string;
  73. begin
  74.   jHttpClient1.AddClientHeader('User-Agent','api-explorer-client') ;
  75.   jHttpClient1.AddClientHeader('Authorization:',atoken) ;  //Authorization
  76.   jHttpClient1.AddClientHeader('Dropbox-API-Arg:','{"path":'+file_source+'}') ;
  77.   jHttpClient1.UploadFormName:='DropBox' ;
  78.   Url := aurldown;
  79.   jHttpClient1.GetAsync (url) ;
  80.   SHOWMESSAGE('wait...');
  81. end;
  82.  
  83. procedure TAndroidModule1.jHttpClient1ContentResult(Sender: TObject;
  84.   content: RawByteString);
  85. var
  86.   list: TStringList;
  87.   jpath:String;
  88. begin
  89.   showmessage('downloanding');
  90.   jpath:=Self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+alocalfile;
  91.    if FileExists(jpath) then
  92.       DeleteFile(jpath) ;
  93.   list:= TStringList.Create;
  94.   list.Text:= content;
  95.   list.SaveToFile(jpath);
  96.   list.Free;
  97.    if FileExists(jpath) then
  98.       showmessage('file exist')
  99.    else
  100.        showmessage('Not Exist');
  101. end;
  102.  
  103.  
  104. procedure TAndroidModule1.jButton2Click(Sender: TObject);
  105. const
  106.   alocalfile='myfile.jpg';
  107.   file_source='"/myfolder/myfile.jpg"';
  108. var
  109.   Url: string;
  110.   ms:TMemoryStream ;
  111.   jpath:String;
  112. begin
  113.   showmessage('uploanding');
  114.   jpath:=Self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+alocalfile;
  115.   jHttpClient1.AddClientHeader('MimeType:','Application/octet-stream') ;
  116.   jHttpClient1.AddClientHeader('UserAgent:','api-explorer-client') ;
  117.   jHttpClient1.AddClientHeader('Authorization:',atoken) ;
  118.   jHttpClient1.AddClientHeader('Dropbox-API-Arg:',' {"path":"'+file_source+'","mode":{".tag":"overwrite"},"autorename":false,"mute":false,"strict_conflict":false}');
  119.   Url :='https://content.dropboxapi.com/2/files/upload/';
  120.   jHttpClient1.UploadFile(url,jpath) ;
  121. //  jHttpClient1.Post(aurldown);
  122.    showmessage(inttostr(jHttpClient1.GetResponseCode))  ;
  123.   showmessage('ok');
  124. end;
  125.  
  126.  
  127. procedure TAndroidModule1.jHttpClient1UploadFinished(Sender: TObject;
  128.   connectionStatusCode: integer; responseMessage: string; fileName: string);
  129. begin
  130.   showmessage('finish upload connectionStatusCode' + inttostr(connectionStatusCode));
  131.   showmessage('finish upload responseMessage'+responseMessage);
  132. end;
  133.  
  134.  
windows 10 64

 

TinyPortal © 2005-2018