Jump to content
houssam1984

i need help with uploading a file from android device to distant server

Recommended Posts

hello brothers , i am trying to make an android app useing delphi 11.3 , the app should read storage where i can select a pdf file the upload it into a distant server

-getting permission is ok

- i asked chatgpt and it gave me a model where the file path is shown in an edit "supposly" , when i run it , it show files but when i select a file nothing happened " i dont get anything in my edit"

- then when i tr wrote the path of the file to upload "MANUALY" the idhttp send can not open file permission denied

-please can any one help me !!!

Share this post


Link to post

What protocols does this distant server support? Which authentication steps need to be taken? Can you manually or by other means do this task? As soon as you can answer these questions...you'll get an educated answer.

  • Like 1

Share this post


Link to post

thank you , its a simple server with http protocol ,  what i wanted is choose pdf file on an android device  click a button "Upload" and the file will be uploaded to the server over http , i am sorry if i couldnt explain my self well

Share this post


Link to post
https://www.youtube.com/watch?v=ouNEdebsq8Y

+

https://github.com/dondonondon/FMXTutorialYoutube/tree/main/Sesi%201/%23012%20Upload%20%26%20Download%20WebService%20.%20RESTAPI%20%26%20Permission

 

Edited by #ifdef

Share this post


Link to post
8 hours ago, houssam1984 said:

- getting permission is ok

- idhttp send can not open file permission denied

That's a contradiction.  Either you have the permissions or you don't.  What does your code look like that is asking the user for permission to access the file, and the code that tries to upload the file?

Share this post


Link to post

 

42 minutes ago, Remy Lebeau said:

That's a contradiction.  Either you have the permissions or you don't.  What does your code look like that is asking the user for permission to access the file, and the code that tries to upload the file?

hello ; permissions are to read storage in my android device ;  if you dont get permission to read storage you cant access  file location 

var
  Intent: JIntent;
 const  PermissionReadExStrg = 'android.permission.READ_EXTERNAL_STORAGE';
begin
 if not PermissionsService.IsPermissionGranted(JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE)) then
  begin
       PermissionsService.RequestPermissions([PermissionReadExStrg],
          procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
           begin
             if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
              { activate or deactivate the location sensor }
               ShowFileChooser
             else
              begin

               ShowMessage('permission not granted');
          end;
        end);
  end
  else
  ShowFileChooser;

 end;

 

 

than showfilechooser procedure looks like :

var
  Intent: JIntent;
  m:TMessageResultNotification;
begin
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_GET_CONTENT);
  Intent.setType(StringToJString('application/pdf'));
  SharedActivity.startActivityForResult(Intent, 0);
 end;

and the uploading procedure looks like

 

var
  HTTP: TIdHTTP;
  FormData: TIdMultiPartFormDataStream;
begin
  if Edit1.Text = '' then
  begin
    ShowMessage('Please select a file.');
    Exit;
  end
  else
  begin

  HTTP :=  TIdHTTP.Create(self);
  FormData := TIdMultiPartFormDataStream.Create;
  try
  
    FormData.AddFile('file', Edit1.Text, 'application/pdf');
    HTTP.Post('my HTTP server', FormData);
    ShowMessage('File uploaded successfully.');
  except
    on E: Exception do
      ShowMessage('Error uploading file: ' + E.Message);
  end;
  FormData.Free;
  HTTP.Free;
  end;

the app runs normally but when i choose the file and click  UPLOAD Button Nothing Happens

Share this post


Link to post
4 hours ago, #ifdef said:

https://www.youtube.com/watch?v=ouNEdebsq8Y

+


https://github.com/dondonondon/FMXTutorialYoutube/tree/main/Sesi%201/%23012%20Upload%20%26%20Download%20WebService%20.%20RESTAPI%20%26%20Permission

 

well the video is great but what i want is to choose a file on my device from sdcard for exemple than upload it to a server over HTTP or REST

Share this post


Link to post

You did not show the code that receives the file path from the file chooser and puts it into the TEdit, or show what the file path actually looks like.  But ACTION_GET_CONTENT is documented as returning a "content:" uri, which requires you to use the ContentResolver class to access the file data.

 

But TIdMultipartFormDataStream.AddFile() uses TFileStream, which wants direct access to the file, which will not work with a "content:" uri on modern Android versions.

 

So you will likely have to either:

  • access the file yourself via Android APIs and read its byte data into a TMemoryStream or TByteStream, or
  • create a custom TStream that wraps an Android InputStream from the ContentResolver.openInputStream() method

And then use TIdMultipartFormDataStream.AddFormField() instead of TIdMultipartFormDataStream.AddFile() to send whichever TStream you end up using.

 

Alternatively, you can use ContentResolver to extract the underlying "file:" uri from a "content:" uri, and then you should be able to open the file using a normal TFileStream (provided you have permissions to the file).

 

Also, when using ACTION_GET_CONTENT, the ACTION_GET_CONTENT documentation says to wrap your Intent with Intent.createChooser(), and also include CATEGORY_OPENABLE and FLAG_GRANT_READ_URI_PERMISSION on your Intent.

Edited by Remy Lebeau
  • Like 3

Share this post


Link to post
4 hours ago, Remy Lebeau said:

You did not show the code that receives the file path from the file chooser and puts it into the TEdit, or show what the file path actually looks like.  But ACTION_GET_CONTENT is documented as returning a "content:" uri, which requires you to use the ContentResolver class to access the file data.

 

But TIdMultipartFormDataStream.AddFile() uses TFileStream, which wants direct access to the file, which will not work with a "content:" uri on modern Android versions.

 

So you will likely have to either:

  • access the file yourself via Android APIs and read its byte data into a TMemoryStream or TByteStream, or
  • create a custom TStream that wraps an Android InputStream from the ContentResolver.openInputStream() method

And then use TIdMultipartFormDataStream.AddFormField() instead of TIdMultipartFormDataStream.AddFile() to send whichever TStream you end up using.

 

Alternatively, you can use ContentResolver to extract the underlying "file:" uri from a "content:" uri, and then you should be able to open the file using a normal TFileStream (provided you have permissions to the file).

 

Also, when using ACTION_GET_CONTENT, the ACTION_GET_CONTENT documentation says to wrap your Intent with Intent.createChooser(), and also include CATEGORY_OPENABLE and FLAG_GRANT_READ_URI_PERMISSION on your Intent.

ups !! i didnt say i am beginner , i partally understand what you said  but if it is not toooooooooo far from me can you provide me any examples of code???

Share this post


Link to post
20 hours ago, houssam1984 said:

ups !! i didnt say i am beginner , i partally understand what you said  but if it is not toooooooooo far from me can you provide me any examples of code???

Sorry, I don't have any examples to give you, as I don't write code for Android.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×