Android Question Delete file with HTTP Server?

William Hunter

Active Member
Licensed User
Longtime User
I have been working with Erel’s HTTP Server example as illustrated in the tutorial, which works very well to download and upload files.

If one were to append a Delete link, after the View link on the list page, would it be possible to delete that file using another library? If so, how would this be done?

Any help greatly appreciated.
 

William Hunter

Active Member
Licensed User
Longtime User
You do not need any library. Just use File.Delete to delete the file.
Thank you Erel. This is my first work with HttpServer and I’m stymied at trying to obtain the path to a file on the server. I am trying to set up a Select Case for the deletion of that file. I haven’t found anything helpful in a search of the forum, and would appreciate a little coaching. The code I am working with is below:
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        Log("Client: " & Request.RemoteAddress)
        Log(Request.RequestURI) 'handle the request based on the URL
        Select True
            Case Request.RequestURI = "/"
                HandleMainPage (Response)
            Case Request.RequestURI.StartsWith("/list/")
                HandleList (Request, Response)
            Case Request.RequestURI.StartsWith("/download/")
                SetContentType(Request.RequestURI, Response)
                Response.SendFile("", DecodePath(Request.RequestURI.SubString(9)))
            ' ########################################################################################
           
            Case Request.RequestURI.StartsWith("/delete/")
                SetContentType(Request.RequestURI, Response)
                Dim CurrentPath As String = DecodePath(Request.RequestURI.SubString("/delete/".Length))
                File.Delete(server.CurrentPath)
               
            ' ########################################################################################                               
            Case Request.RequestURI.StartsWith("/upload/")
                HandleUpload(Request, Response)
            Case Else
                'send a file as a response (this section is enough in order to host a site)
                SetContentType(Request.RequestURI, Response)
                Response.SendFile(File.DirAssets, DecodePath(Request.RequestURI.SubString(1)))
        End Select
    Catch
        Response.Status = 500
        Log("Error serving request: " & LastException)
        Response.SendString("Error serving request: " & LastException)
    End Try
End Sub
What is the correct syntax to find the file path, and then to delete the file? Help would be appreciated.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Something like:
B4X:
Dim fileToDelete As String = DecodePath(Request.RequestURI.SubString(7))

File.Delete(Server.CurrentPath, fileToDelete)
Thank you Erel. When I try your suggestion I get the error message “Unknown member: currentpath”. If I remove CurrentPath from the File.Delete statement I get the error message “Unknown member: filetodelete”. Do you have any other suggestion? The code I am using is below:
B4X:
Case Request.RequestURI.StartsWith("/delete/")
      SetContentType(Request.RequestURI, Response)
      Dim fileToDelete As String = DecodePath(Request.RequestURI.SubString(7))
      File.Delete(server.CurrentPath, fileToDelete)
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
I took it from the code you posted in post #3.

Use the debugger to see how the uploading code works. It should be very similar.
Thank you for your reply Erel. I am using B4A – Bridge with a view of seeing how the various variables for view (download) and upload are constructed. This is my first use of B4A – Bridge and it seems that none of the Log points that I have in place appear in the log.

Is this normal behavior? If so, is there some other means of setting check points to allow the viewing of certain actions initiated by the code?

What I had thought would be an easy chore now has me stumped. I am almost thinking that DeleteFile would have to be a function of HttpServer in order to delete files. Any attempt that I have made with code in ServerService, just doesn’t work. Any help would be greatly appreciated.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
We are making headway. I can now delete files. I am using the code below in Sub HandleList to build the file list. This lets me download or delete a file, but gives me two lines for each file. One for download and one for delete. I would like to have one line per file incorporating these two links. I haven’t found a way of doing this that doesn't both download and delete the file with a single click on either link.

Can someone help me with the proper syntax to include both links on the same line? Any help is greatly appreciated.
B4X:
For Each F As String In files ' this code allows for either download or deletion
        listSb.Append("<li>")
        listSb.Append(F).Append(" (").Append("<a href='/download/").Append(EncodePath(File.Combine(CurrentPath, F))).Append("'>Download</a>)")
        listSb.Append("<li>")
        listSb.Append(F).Append(" (").Append("<a href='/delete/").Append(EncodePath(File.Combine(CurrentPath, F))).Append("'>Delete</a>)")
        listSb.Append("</li>")
Next
Regards
EDIT - I took a break, sipped on a nice glass of wine, and had an epiphany. All my issues have been resolved, and I have everything working as required. Thank you for your pointers Erel. No further assistance is required.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…