Avatar of Wayne Barron
Wayne BarronFlag for United States of America

asked on 

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

ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Wayne Barron
SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
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.
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

ASKER

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.
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

ASKER

Good idea on the (give hour in 24H)
ASKER CERTIFIED SOLUTION
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

ASKER

Fixed it.
However, I am giving Nitin the points for the HH thing.
Best to have 24 instead of 12.

All is good.
Wayne
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo