vb.net upload multiple files script, duplicates all files

Hello All;

I have the following code, of which uploads multiple files at one time.
The issue is this.

I am renaming the files to the current date/time format.ext ......
With this renaming, it is naming the files appropriately, but it is duplicating the files in the upload folder.

Could someone please, take a look at this code, and let me know what is going on with it?

Thank You
Carrzkiss

Upload Form.
<asp:FileUpload ID="filMyFile" runat="server" AllowMultiple="true" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick ="UploadMultipleFiles" accept ="image/gif, image/jpeg" />
<hr />
<asp:Label ID="lblSuccess" runat="server" ForeColor ="Green" />

Open in new window


CodeBehind
Imports System.IO

Partial Class _Default
    Inherits Page



    ' We are going to Add in a Global Class, this will hold ALL the variables that need to be used through the script.
    Public Class GlobalVariables
        Public Shared getfile As String
    End Class


    Protected Sub UploadMultipleFiles(sender As Object, e As EventArgs) Handles btnUpload.Click
        For Each postedFile As HttpPostedFile In filMyFile.PostedFiles


            ' We are going to rename the file, so it is NOT a long file name.
            ' This will give the following format: 02191971115523 (February 19 1971 11:55:23 + milliseconds)
            Dim getTime As String = Now.ToString("MM") + Now.ToString("dd") + Now.ToString("yyyy") + Now.ToString("hh") + Now.ToString("mm") + Now.ToString("ss") + Now.ToString("fff")
            GlobalVariables.getfile = getTime

            'save the file
            Dim savepath As String = ""
            savepath = Context.Server.MapPath("Files") '+ Artist_Name/Album_Name (This will be added later on)
            If Not Directory.Exists(savepath) Then
                Directory.CreateDirectory(savepath)
            End If

            ' We are going to get the Extention of the MP3, just so we can make sure that it is prefixed onto the file.
            Dim extension As String = Path.GetExtension(filMyFile.PostedFile.FileName)

            ' This is where we will save the file. This is where the magic happens.
            filMyFile.PostedFile.SaveAs(Convert.ToString(savepath & "\") + Convert.ToString(GlobalVariables.getfile) & extension)

        Next
        '  End If
        lblSuccess.Text = String.Format("{0} files have been uploaded successfully.", filMyFile.PostedFiles.Count)
        '  End If
    End Sub
End Class

Open in new window

LVL 31
Wayne BarronAuthor, Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
You didn't mention WHEN the it is getting duplicated. Are you getting error once in a while?

There is nothing hugely inappropriate with code, that I can spot. All I could see that may be you wish to use HH instead of hh which will give hour in 24H format.
0
Nitin SontakkeDeveloperCommented:
I didn't understand the assignment to GlobalVariables and then taking it back from there. I suggest don't do it unless ABSOLUTELY necessary, I cannot see the reason.
0
Wayne BarronAuthor, Web DeveloperAuthor Commented:
When the file is uploaded to the server, it is duplicated.
All you have to do is make a new "Website" and run this code, and you will see.

As for the GlobalVariable, it is was supposed to be removed.
As it was not needed in this project. Just did not get it out of the example.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Wayne BarronAuthor, Web DeveloperAuthor Commented:
Good idea on the (give hour in 24H)
0
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Fixed it.
I got it working in this example, I just need to recode my actual project to mimic this example.
But it runs and only uploads one file, and not duplicating files.

I removed the extension and replaced it with the filename of  ".mp3"
As that is what I am uploaded to the server.
And all seems to work now.

What I did was went back to the original code from here. aspsnippets
And started over with that code, and then slowly added in the renaming.
When I did the extension, it duplicated the files.
So, I removed it and replaced it with the .mp3 and everything went to working.

I am not really sure what the issue is with the extension being there, but it seemed to cause it to duplicate the files.

So, here is the working CodeBehind.
Wayne

===========UPDATE=========
In the Event Handler, the end of the line has
>>>>   Handles btnUpload.Click
This it what was causing the duplicate.


    Protected Sub UploadMultipleFiles(sender As Object, e As EventArgs) ' Handles btnUpload.Click  <==== This was causing the duplicates.
        For Each postedFile As HttpPostedFile In filMyFile.PostedFiles
            Dim fileName As String = Path.GetFileName(postedFile.FileName)


            Dim getTime As String = Now.ToString("MM") + Now.ToString("dd") + Now.ToString("yyyy") + Now.ToString("HH") + Now.ToString("mm") + Now.ToString("ss") + Now.ToString("fff")
            fileName = getTime

            Dim savepath As String = ""
            savepath = Context.Server.MapPath("Files")
            If Not Directory.Exists(savepath) Then
                Directory.CreateDirectory(savepath)
            End If

            postedFile.SaveAs(Convert.ToString(savepath & "\") + Convert.ToString(fileName & ".mp3"))
        Next
        lblSuccess.Text = String.Format("{0} files have been uploaded successfully.", filMyFile.PostedFiles.Count)
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Fixed it.
However, I am giving Nitin the points for the HH thing.
Best to have 24 instead of 12.

All is good.
Wayne
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.