troubleshooting Question

vb.net upload multiple files script, duplicates all files

Avatar of Wayne Barron
Wayne BarronFlag for United States of America asked on
ASP.NETVisual Basic.NET
6 Comments2 Solutions287 ViewsLast Modified:
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" />

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
ASKER CERTIFIED SOLUTION
Wayne Barron
Author, Web Developer
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros