Android Question Sending a file from the user device

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

I have an application for electronic exams, the user has to create an excel file on his computer, copy the file to his phone, and then upload it through the application. Below is the code I used, and it did not succeed in uploading the file.

B4X:
cc.Show("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "اختر ملف")
    Wait For cc_Result (Success As Boolean, Dir As String, FileName As String)
    Dim xui As XUI
    If Success=True Then
        Dim in As InputStream
        in = File.OpenInput(Dir, FileName)
        Dim out As OutputStream
        out = File.OpenOutput(xui.DefaultFolder, "uploaded_file.xlsx", False)
        File.Copy2(in, out)
        in.Close
        out.Close
        Up.doFileUpload(Null,Null,xui.DefaultFolder & "/uploaded_file.xlsx","https://mywebsite.com/Delta/upload_file.php")
    End If

Maybe the problem was with the permission to access the storage.

What is wrong with the code?
Secondly, I want to know, is this the best way? And if there is another way that bypasses asking the user for permission to access the storage (other than sending via email).
 

drgottjr

Expert
Licensed User
Longtime User
1) you say "it did not succeed in uploading the file", but you did not show the code you used to upload the file. the code you did show has nothing to do with an upload.

2) you do not say whether or not you were able to copy the file from a system folder to your app for upload using the contentchooser. was that part successful or not. if not, what was the error?

3) when using a method which can return success or failure, you should check for both. obviously, if you only test for success, and success is false, then any code in that block will not run, but the user will not know what is happening.
 
Upvote 0

khwarizmi

Active Member
Licensed User
Longtime User
This is the php code:
B4X:
<?php
 
    $file_path = "Menus/";
   
    $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";
    }
 ?>
The file was specified by contentchooser, but I couldn't tell if it was copied or not.
I test it by:
B4X:
if File.Exists((xui.DefaultFolder, "uploaded_file.xlsx")
It gives True, But I doubt, if the file was in real mode the upload would have succeeded.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
It seems the library UploadFilePhp does not support https. the code works with http.
1. You need to contact the author of this library to add HTTPS support.
2. Or you can use okhttpUtils2 for file upload without the library
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…