Android Question Upload file using multipartpost

yes

Member
Licensed User
Using code provided Manfred in https://www.b4x.com/android/forum/threads/upload-file-with-php.37562/#post-221947 , i try to upload a file but error occured. can any1 help me ? i did not change anything to the code except the url of server.

Please update to B4A-Bridge v2.30+
Logger connected to: samsung SM-J710GN-358447073659196
--------- beginning of main
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
Error occurred on line: 36 (MultipartPost)
java.io.FileNotFoundException: /data/user/0/b4a.example.multipartpost/files/virtual_assets/snap1.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:199)
at b4a.example.multipartpost.multipartpost._createpostrequest(multipartpost.java:127)
at b4a.example.multipartpost.main._activity_create(main.java:455)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.multipartpost.main.afterFirstLayout(main.java:102)
at b4a.example.multipartpost.main.access$000(main.java:17)
at b4a.example.multipartpost.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 20 more
 

DonManfred

Expert
Licensed User
Longtime User
I use the same code and it is working for me. For sure i send only files which exists.

Based on the error the file your want to upload does not exists.
Upload a example project which shows the issue.
 
Upvote 0

yes

Member
Licensed User
I use the same code and it is working for me. For sure i send only files which exists.

Based on the error the file your want to upload does not exists.
Upload a example project which shows the issue.

How did you create the file? the file should be name as coca_cola but where i suppose to create the file? i'm new in programmin
Dim fd As FileData
fd.Initialize
fd.Dir = File.DirAssets
fd.FileName = "coca_cola.png"
fd.KeyName = "coca_cola"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How did you create the file?
i copied the file to the right place.
the file should be name as coca_cola but where i suppose to create the file?
It does NOT CREATE anything. The methods does Upload AN ALREADY EXISTENT Image/file.
If you do not have a coca_cola.jpg then create one. Or use the name from an existing file....

i'm new in programmin
then i guess you should start with the beginners guide... See https://www.b4x.com/b4a.html for the download
 
Upvote 0

yes

Member
Licensed User
i copied the file to the right place.

It does NOT CREATE anything. The methods does Upload AN ALREADY EXISTENT Image/file.
If you do not have a coca_cola.jpg then create one. Or use the name from an existing file....

I know it do not create anything , it should be programmer create the file and i understand the code but my problem is where should i create the file? in phone folder or multipartpost folder? can u give the exact location of coca_cola file should be?
 
Upvote 0

yes

Member
Licensed User
Based on the error message you have set the file name to snap1.png not coca_cola.jpg.

Thank you Erel. i did create the file as the code needed and i wrote the name exactly as the code. however it still not working and i noticed that my file in asset folder was not installed to the device instead it got deleted during execution, any advice to solve this?
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
the File to Upload must exsist! Test it with File.Exist....

Upload like this:
B4X:
dim hc as okhttpclient
hc.InitializeAcceptAll("hc")  
  
Dim files As List
Dim fd As FileData
Dim NV As Map

    files.Initialize
    fd.Initialize
    NV.Initialize
  
    fd.Dir = 'the Phat, e.g. file.dirassets
    fd.FileName = 'the Filename
    fd.KeyName = "filename"
    fd.ContentType = "application/octet-stream"  'change to your FileType
    files.Add(fd)
  
    Dim req As OkHttpRequest
    req = MultipartPost.CreatePostRequest(url_GMA_Up, NV, files)
    hc.Execute(req, 1)
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
a little bit OT (update),

I see that I use the following code to select one of the files (user can copy them to edit)

B4X:
    If File.Exits..... = True
        Log("Liste Extern")
    Else If File.Exists(File.DirAssets,"liste.txt") = True Then 
        Log("Liste Intern")
        Liste2 = File.readlist(File.DirAssets,"liste.txt")
    end if

it works at me with File only at DirAssets.
 
Upvote 0
Top