Hello everyone,
Here’s the code I put in UploadFinished
But as there will be several sessions at the same time and users will send their photo taken with their smartphone, the name of the file will be by default “image.jpg”, so I would like to add code so that before saving, check if there is not a file with the same name and if so add the time, minutes, seconds or other name to make them unique, do you have an idea?
Thank you very much!
Dim uploadFolder As folderItem
uploadFolder = App.ExecutableFile.Parent.Child(“UploadedPictures”)
If Not uploadFolder.Exists Then
#If DebugBuild Then
uploadFolder.CreateFolder
#Else
MsgBox(“Upload failed”)
Return
#Endif
End If
Dim uploadedPicture As Picture
Dim savePicture As FolderItem
For Each uFile As WebUploadedFile In files
Try
If uFile.file <> Nil Then
uploadedPicture = Picture.Open(uFile.File)
Else
uploadedPicture = Picture.FromData(uFile.data)
End If
// Save the file to disk
savePicture = uploadFolder.Child(uFile.Name)
uFile.Save(savePicture)
Catch err As IOException
Exit For
End Try
Next
MessageBox(“ok”)
Basic idea something like this
// Save the file to disk
savePicture = uploadFolder.Child(uFile.Name)
If savePicture.Exists Then
savePicture = uploadsFolder.Child(uFile.Name+"_"+DateTime.Now.SQLDateTime)
End
uFile.Save(savePicture)
Or just add the timestamp to all pictures regardless of whether the name exists or not.
This may fail if two images with the same name are uploaded within a second of each other since SQLDateTime has a resolution of one second. If that’s an issue you could use SecondsFrom1970, or NanoSecond, or a random number.
Edit: As @TimStreater shows, you probably want to insert the unique identifier before the file’s extension - my code above appends it to the full filename.
1 Like
I use a random number, as follows:
if (sourceFile<>Nil and sourceFile.Exists=True) then // Make sure source file still available
parts = splitExt (filename)
afn = fdestination.NativePath + app.dirch + filename // Destination file name
while (True) // Avoid overwriting existing file
atFile = new FolderItem (afn, FolderItem.PathModes.Native) // Create new filename if required
if (atFile=Nil) then Exit
if (atFile.Exists=False) then Exit // This one does not exist, use it
tmpfn = parts(0) + "-" + App.Randomiser.InRange(10000, 99999).ToString
if (parts(1).Length>0) then tmpfn = tmpfn + "." + parts(1)
afn = fdestination.NativePath + app.dirch + tmpfn // New candidate destination filename
wend
This works with no issues.
1 Like
Thanks for your answers, but I found this one too:
Var d As DateTime = DateTime.Now
Var s As String = Session.identifier
// Save the file to disk
savePicture = uploadFolder.Child(s.LeftBytes(5) + d.Hour.ToString + d.Minute.ToString + d.Second.ToString + uFile.Name)
uFile.Save(savePicture)