B4J Code Snippet [ABMaterial] FileUpload Multipart to original filename

Hello.
When we load files with the same name (FileUpload), the files named MultiPartxxxxxxxx appear in the / uploads directory. However, it happens to me that the user loads a file with the same name and I did not know what he was up to. I wrote a change that changes.

Modified ABMUploadHandler file

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    'get the callback page from the session (multiple modules can use this handler)
    Dim callback As Object = req.GetSession.GetAttribute("abmcallback")
    Dim downloadfolder As String = File.Combine(File.DirApp, req.GetSession.GetAttribute("abmdownloadfolder"))
    Dim MaxSize As Int
    Try
        MaxSize = req.GetSession.GetAttribute("abmmaxsize")
    Catch
        resp.SendError(500, LastException.Message)
        Return
    End Try
    Dim data As Map
    Dim fileName,fileName2 As String 'add new variable fileName2
    Dim tmpFileName As String
   
    Try
        data = req.GetMultipartData(downloadfolder, MaxSize)
        Dim filePart As Part = data.Get("upl")
        If filePart.IsInitialized Then
            fileName =     filePart.SubmittedFilename
            tmpFileName = filePart.TempFile
            ' add code
            fileName2 = File.GetName(tmpFileName)
            req.GetSession.SetAttribute("MultiPart", fileName2)
            ' end new code
            If ABM.HandleUpload(downloadfolder, tmpFileName, fileName) Then
                req.GetSession.SetAttribute("MultiPart", fileName)
               
                If SubExists(callback, "Page_FileUploaded") Then
                    CallSubDelayed3(callback, "Page_FileUploaded", fileName, True)
                End If
            Else
                If SubExists(callback, "Page_FileUploaded") Then
                    CallSubDelayed3(callback, "Page_FileUploaded", fileName, False)
                End If
            End If
        Else ' try image
            Dim filePart As Part = data.Get("imageFromCanvas")
            fileName = filePart.SubmittedFilename
            tmpFileName = filePart.TempFile
            'add new code
            fileName2 = File.GetName(tmpFileName)
            req.GetSession.SetAttribute("MultiPart", fileName2)
            'end new code
            If ABM.HandleUpload(downloadfolder, tmpFileName, fileName) Then
                If SubExists(callback, "Page_FileUploaded") Then
                    req.GetSession.SetAttribute("MultiPart", fileName)
                    CallSubDelayed3(callback, "Page_FileUploaded", fileName, True)
                End If
            Else
                If SubExists(callback, "Page_FileUploaded") Then
                    CallSubDelayed3(callback, "Page_FileUploaded", fileName, False)
                End If
            End If
        End If
       
    Catch
        resp.SendError(500, LastException.Message)
        If SubExists(callback, "Page_FileUploaded") Then
            CallSubDelayed3(callback, "Page_FileUploaded", LastException.Message , False)
        End If
    End Try
End Sub

And changes in 'Sub Page_FileUploaded'
B4X:
    Dim multi As String= ws.Session.GetAttribute2("MultiPart", FileName)
        If multi <> FileName Then
            Dim time As String = DateTime.GetYear(DateTime.Now)&DateTime.GetMonth(DateTime.Now)&DateTime.GetDayOfMonth(DateTime.Now)&DateTime.GetHour(DateTime.Now)&DateTime.GetMinute(DateTime.Now)&DateTime.GetSecond(DateTime.Now)
            Dim source As String = File.DirApp& DownloadFolder
            source = File.Combine(source,"")
            File.Copy(source, multi, source, time&FileName)
            File.Delete(File.DirApp&DownloadFolder,multi)
            hdd_file(1) = time&FileName
            original_file(1) = FileName
            ws.Session.RemoveAttribute("MultiPart")
        Else
            '......
        End If
        Log(FileName & " = " & success)
        page.ws.Flush 'IMPORTANT


After these changes we will upload the same files, but before the original file name is inserted in the form of year_month_day_hour_minute_second. This way we can divide the same files.
 
Last edited:

Hanz

Active Member
what are these two lines? Are they suppose to be included? They are not declared:

hdd_file(1) = time&FileName
original_file(1) = FileName
 

MichalK73

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Page_FileUploaded(FileName As String, success As Boolean)
    Dim p As ABMContainer = page.Component("window")
    Dim img As ABMImage = p.Component("image")
    Dim hdd_file As String
    Dim original_file As String
    Dim multi As String= ws.Session.GetAttribute2("MultiPart", FileName)
    If multi <> FileName Then
        Dim time As String = DateTime.GetYear(DateTime.Now)&DateTime.GetMonth(DateTime.Now)&DateTime.GetDayOfMonth(DateTime.Now)&DateTime.GetHour(DateTime.Now)&DateTime.GetMinute(DateTime.Now)&DateTime.GetSecond(DateTime.Now)
        Dim source As String = File.DirApp& DownloadFolder
        source = File.Combine(source,"")
        File.Copy(source, multi, source, time&FileName)
        File.Delete(File.DirApp&DownloadFolder,multi)
        hdd_file = time&FileName
        original_file = FileName
        ws.Session.RemoveAttribute("MultiPart")
        img.Source = "../uploads/"&hdd_file
    Else
        img.Source = "../uploads/"&FileName
    End If
    
    img.Refresh

    Log(FileName & " = " & success)
    page.ShowToast("toast" , "toastorange", "Uploading " & FileName & ": Success = " & success, 3000, False)
    page.ws.Flush 'IMPORTANT
End Sub
 
Top