I have this code below that uploads a file to a folder. The problem is that it will overwrite an existing file there with the same name. How can I avoid this, so if there is a file there called name.doc, it would rename the next one uploaded to basically anything but name.doc? name1.doc, namea.doc, whatever... it doesn't matter to me.
Thanks
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If (upImage.HasFile) Then
If (CheckFileType(upImage.FileName)) Then
Dim filePath As String = "~/census/" & upImage.FileName
upImage.SaveAs(MapPath(filePath))
End If
End If
End Sub
Function CheckFileType(ByVal fileName As String) As Boolean
Dim ext As String = Path.GetExtension(fileName)
Select Case ext.ToLower()
Case ".pdf"
Return True
Case ".doc"
Return True
Case ".docx"
Return True
Case ".xls"
Return True
Case ".xlsx"
Return True
Case ".tif"
Return True
Case ".gif"
Return True
Case Else
MultiView1.SetActiveView(View4)
lblError.Text = "Invalid File Extension"
Return False
End Select
End Function
Sub Page_PreRender()
Dim upFolder As String = MapPath("~/census/")
Dim dir As New DirectoryInfo(upFolder)
dlstImages.DataSource = dir.GetFiles()
dlstImages.DataBind()
End Sub