Android Question Tips for uploading images to web hosting.

vecino

Well-Known Member
Licensed User
Longtime User
Hello, an application that I have been asked, the user can upload images to a web hosting.
I have seen several threads on the subject, but I would like advice on the most comfortable and functional way to do it.

Among my doubts, I seem to understand that it is necessary a php program in the hosting to receive the images, is it so?

Please, I need advice.
Thank you very much.
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I usually use a php application to receive the image and send with PostMultiPart. Depending on the type of hosting you could also use a B4j App.

There is also a method using a webview
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hi folks, I have not managed to get it to work yet.
I don't know if these examples are a bit complex for what I need.
Just in case, I clarify that I only need to send an image from an internal folder of my application.
UploadImage('images/foto1.png')
It is also not clear to me if I need to use some php on the hosting.
Isn't there a simpler example?
Thank you.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
do you control the server? if so, php might be a convenient approach for you. if you do not control the server, then you need to find out what the server is expecting.

for some reason, it is assumed you will be using webview to upload the files. is this the case? if not, you would use okhttputils2, for which there are many, many examples here in the forum. a simpl file upload only requires okhttputils2 and whatever additional data might be required by the server (eg, password, etc...)
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hi, yes, I have access to the hosting, it is a linux with apache or nginx.
I guess okhttputils2 will be better, I don't want to call an external application (webview) .
Do you recommend me any thread on the forums where it is done that way?
Thanks.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
What about FTP?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
still a little unclear about what you're doing. you have "access" to hosting; do you know how to use php? both apahce and nginx support it. php.net might be the best place for you to look for instructions. they will depend on the version of php that the server is running. this part of your project has nothing to do with b4x.

ftp, as suggested by don jose aguilar is another possibility (in some ways simpler on both ends), but not all servers allow it. it's an older protocol with some issues. but you need to ask your server's administrator. also, where are these images being uploaded to? all the users share the same directory for uploads? if users all have their own directories, you need to sort that out. and i'm guessing that if you're having people upload images, they're probably going to want to see them or share them... if you decide to try ftp, there are examples. just use the search box.

as for b4x, here's one example with okhttputils2 and postmultipart:
https://www.b4x.com/android/forum/threads/postmultipart-on-okhttputils2.76648/
but there are many others
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
What about FTP?
This client had some serious trouble with another application that used FTP and when I mentioned the word "FTP" they freaked out. I don't know what happened to them but they refused to use that protocol.
So I offered them http.
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
In my app
B4X:
public Sub UploadMedia
    
    Try
        
        
        Dim MySQL As String
        Dim Cursor1 As Cursor
        Dim Qty As Int
        Dim IsFTPOk As Boolean
        
        MySQL="select ID, FileName from tblNotesMediaFiles "
        MySQL= MySQL & "where PlacementLocalID=? And PlacementID=? And JobDate=? And Shift=? And ifnull(FTPDate,'1/1/1900')='1/1/1900'"
            
        FileName=""
        
        Cursor1=SQL1.ExecQuery2(MySQL,Array As String(Main.SelectedPlacementLocalID, Main.SelectedPlacementID, Main.SelectedDOS,Main.SelectedShift))
        
        Qty=Cursor1.RowCount
        
        Dim files As List,Records As List
        Dim job As HttpJob
        
        files.Initialize
        Records.Initialize
        
        
        For i =0 To Qty-1
            
            Cursor1.Position=i
            
            FileName=Cursor1.GetString("FileName")
                        
            If FileName<>"" Then
        
                If File.Exists(Starter.FileDir,FileName) Then
            
                    RecordID=Cursor1.GetInt("ID")
                    Records.Add(RecordID)
                    
                    Dim fd As MultipartFileData
                    fd.Initialize
                    
                    If FileName.Contains("jpg") Then
                        fd.KeyName = "imagejpg"
                        fd.ContentType = "image/jpg"
                    Else
                        fd.KeyName = "audiowav"
                        fd.ContentType = "audio/wav"
                    End If
                    
                    fd.FileName = FileName
                    fd.Dir = Starter.FileDir
                    
                    files.Add(fd)
                    
                End If
            End If
            
        Next
        
        Cursor1.Close
        
        If files.Size>0 Then
        
            For j=0 To files.Size-1
                
                Dim CurrentFile As List,CurrentRecord As List
                CurrentFile.Initialize
                CurrentRecord.Initialize
            
                CurrentFile.Add(files.Get(j))
                CurrentRecord.Add(Records.Get(j))
                    
                Dim job As HttpJob
                job.Initialize("Upload",Me)
                
                
        
                job.PostMultipart(modFun.URL ,CreateMap("action":"upload", "Ticket":Main.Ticket,"RegKey":Main.RegKey,"WhatToDo":"receivefiles"),CurrentFile)
        
                Wait For (job) JobDone(job As HttpJob)
        
                If job.Success Then
            
                    Dim response As String
            
                    response=job.GetString
            
                    Log("File was uploaded successfully " & job.GetString )
            
                    
            
                    If IsFTPOk=False Then
                        modFun.ShowError("clsRefreshJobs_File upload has failed " & response & CRLF & modFun.NoConnection(Main.SQL1))
                    End If

                Else
                    modFun.ShowError("clsRefreshJobs_HTTP Upload error " & LastException.Message & CRLF & modFun.NoConnection(Main.SQL1))
                    IsFTPOk=False
                End If
        
                job.Release
            
            Next
            
        
        End If
        
        If IsFTPOk=False Then
            Main.TestRet=False
        End If
        
        CallSubDelayed(Me,"UploadMedia_Complete")
                
    Catch
        ProgressDialogHide
        Log("UploadMedia " &   LastException.Message)
        
    End Try
    
End Sub


and on the server ASP.NET

B4X:
 Dim UploadPath As String = "", URL As String = ""
                Dim filename As String
                Dim QQ As String = ""

                URL = System.Configuration.ConfigurationManager.AppSettings("UploadIMG").ToString
                UploadPath = HttpContext.Current.Server.MapPath(URL)

                Dim File As System.Web.HttpPostedFile               

                For i As Integer = 0 To FileCollection.Count - 1

                    File = FileCollection(i)

                    filename = Path.GetFileName(File.FileName)

                    File.SaveAs(UploadPath & "\" & filename)

                Next
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
So I offered them http.
just be clear what you mean by "http". the client may think you're referring to something in particular. http is just a protocol (implemented in lots of different software)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…