Welcome to WebmasterWorld Guest from 54.227.48.147

Forum Moderators: ocean10000

Message Too Old, No Replies

file upload

need a little help

     
7:38 pm on Sep 20, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts:429
votes: 0


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
8:31 pm on Sept 20, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 12, 2002
posts:1482
votes: 0


This code has not been tested and is purely from the top of my head, but should point you in the right direction.

Before you do upImage.SaveAs(MapPath(filePath)), do:


If System.IO.File.Exists(filePath) Then
Dim fileParts As Array = filePath.Split("."c)

' Only change the filename, without the extension
Dim newFileName As String = fileParts(0) & "-1"

' Recreate the file name
filepath = newFileName & "." & fileParts(1)
End If

upImage.SaveAs(MapPath(filePath))
3:06 pm on Oct 11, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts:429
votes: 0


I got pulled away from this project basically the day I posted this. I am back on it... let me give this a shot now... thanks
5:29 pm on Oct 11, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts:429
votes: 0


OK, tried it out.. Code executes without errors, but doesn't do anything besides upload the file... and will overwrite what is there. Any ideas?
6:18 pm on Oct 11, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 24, 2005
posts:429
votes: 0


I wound up just doing this. So if I were to upload a file called "mysa.gif" it would be saved as "1011201021317-mysa.gif" If you uploaded the same file type, with the same name, 7 seconds later it would be "1011201021324-mysa.gif" In my case, with this application it will be virtually impossible for somebody to upload the same file type and name at exactly the same moment... It works.... I suppose I could add a random number to it too to play it safe, but at most I think 10 people a day will use this... hopefully not ALL at 9:00:00am with the same file name and type!


If (upImage.HasFile) Then
If (CheckFileType(upImage.FileName)) Then


randomnumber = Now
randomnumber = Replace(randomnumber, "/", "")
randomnumber = Replace(randomnumber, ":", "")
randomnumber = Replace(randomnumber, "AM", "")
randomnumber = Replace(randomnumber, "PM", "")
randomnumber = Replace(randomnumber, " ", "")

filename1 = upImage.FileName
filepath = "/census/" & randomnumber & "-" & filename1


upImage.SaveAs(MapPath(filepath))
End If
End If