Solved

Increment file name if it exists on file upload

Posted on 2012-04-05
3
688 Views
Last Modified: 2012-06-21
Hi

I am creating a video uploading website. I would like to avoid having files overwitten when they have the same file name. is there a way to automatically increment the filename if it exists?

My current code is just uploading:

Thanks

'declare the file path to store uploads
        Dim path As String = Server.MapPath("~/UploadedVideos/" & UserLogin.UserNo & "/")

        FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
0
Comment
Question by:ntd1
  • 2
3 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 37813959
If IO.Exists(path & FileUploadVideo.FileName) Then
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName + 1)
Else
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
End If

Open in new window


Personally I would generate a random string, and append it to the filename.  Lemme see if I can drum up something official :)
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 500 total points
ID: 37813991
See, I usually do something like this:
                    Dim sFileName As String = GenString() & System.IO.Path.GetFileName(FileUploadVideo.FileName)

If IO.Exists(path & FileUploadVideo.FileName) Then
    FileUploadVideo.PostedFile.SaveAs(path &  sFileName)
Else
    FileUploadVideo.PostedFile.SaveAs(path & FileUploadVideo.FileName)
End If
                                            

    Private Function GenString(Optional ByVal _MinLength As Integer = 4, Optional ByVal _MaxLength As Integer = 15) As String
        Dim _RandBytes(10) As Byte, _RetStr As String = String.Empty
        Dim _Seed As Integer = CType(Date.Now.Ticks And Integer.MaxValue, Integer)
        Dim _Rand As Random = New Random(_Seed)
        _Rand.NextBytes(_RandBytes)
        _RetStr = Convert.ToBase64String(_RandBytes).Substring(0, _Rand.Next(_MinLength, _MaxLength))
        _RetStr = _RetStr.Replace("+", String.Empty)
        _RetStr = _RetStr.Replace("/", String.Empty)
        _RetStr = _RetStr.Replace("=", String.Empty)
        Return _RetStr
        _Rand = Nothing : _Seed = 0 : Erase _RandBytes : _RetStr = String.Empty
    End Function

Open in new window

0
 

Author Closing Comment

by:ntd1
ID: 37814191
Very Good.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now