Android Question How to upload documents to localhost server in b4a

Muneebx

Member
I just want to upload a file from my mobile phone to localhost on a button click and i've been searchig it on the internet since one week but couldn't find any help, is there anybody who knows how to do it?
Thanks in advance
 

jahswant

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
I just want to upload a file from my mobile phone to localhost on a button click and i've been searchig it on the internet since one week but couldn't find any help, is there anybody who knows how to do it?
Thanks in advance
Use this library , download it, put the .jar and .xml file in additional libraries file

B4X code:

B4X:
 Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Up As UploadFilePhp
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    If FirstTime Then
        Up.Initialize("Up")
    End If
End Sub
Private Sub Button1_click
   phpUrl="http://localhost/your_folder/upload_file.php"
   Dim phoneDocPath As String = File.DirInternal & "/"  'OR  "/sdcard/
   Dim docName As String="xxxxxx.xxx"
   Up.doFileUpload( Null,Null,phoneDocPath & docName, phpUrl)
End Sub

Use Wampserver or XAMP and make a folder under www or htdocs directory put the following php file (name it upload_file.php) in that folder
and inside that folder make the 'uploads' folder where you upload your file..
PHP code:

B4X:
<?php

    $file_path = "uploads/";
    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }

?>

At last, run your b4a app and I hope your document will be uploaded to the localhost server.
 
Upvote 0
Top