mercredi 6 mai 2015

Image name rewrites the old one VB.NET - How to rename same image to a different name every time when upload the file?

I know how to rename a file, but, in this case i have a tool to upload one by one, a lot of images, and, when you try to upload two different images with the same name rewrites the old one.

I need to generate dinamically something to identify every upload as unique, but i dont know how to put a count++ in Vb.net,

thank you very much for your time and consideration.

Dim Upload2 As UploadDetail = DirectCast(Me.Session("UploadDetail"), UploadDetail)

Upload2.IsReady = False
        If fileUpload1.PostedFile IsNot Nothing AndAlso fileUpload1.PostedFile.ContentLength > 0 Then

Dim path__1 As String = UPLOADFOLDER & "/"
Dim fileOK As Boolean = False
Dim fileName As String = Path.GetFileName(fileUpload1.PostedFile.FileName)
Dim strPath As String = Path.Combine(path__1, fileName)

If fileUpload1.HasFile Then

Dim fileExtension As String
fileExtension = System.IO.Path. _
                    GetExtension(fileUpload1.FileName).ToLower()

Dim allowedExtensions As String() = _
{".jpg", ".jpeg", ".png", ".gif"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                Next
                If fileOK Then
                    Upload2.ContentLength = fileUpload1.PostedFile.ContentLength
                    Upload2.FileName = fileName
                    Upload2.UploadedLength = 0
                    Upload2.IsReady = True
Dim bufferSize As Integer = 1
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
                    Using fs As New FileStream(Path.Combine(HttpContext.Current.Server.MapPath(path__1), fileName), FileMode.Create
                        While Upload2.UploadedLength < Upload2.ContentLength

Dim bytes As Integer = fileUpload1.PostedFile.InputStream.Read(Buffer, 0, bufferSize)
                            fs.Write(buffer, 0, bytes)
                            Upload2.UploadedLength += bytes
                        End While
                    End Using

Const js As String = "onComplete(1,'File uploaded correctly.','{0}','{1} of {2} Bytes');"
                    scriptManager.RegisterStartupScript(Me, GetType(FileUploadFotosPDF), "progress", String.Format(js, fileName, Upload2.UploadedLength, Upload2.ContentLength), True)
                    Session("Enabled") = fileName
                Else

Const js As String = "onComplete(4, 'There was a problem with the file. Perhaps, this file is not an image or corrupt','','0 of 0 Bytes');"
                    scriptManager.RegisterStartupScript(Me, GetType(FileUploadFotosPDF), "progress", js, True)
                End If

                Upload2.IsReady = False
            Else
Const js As String = "onComplete(4,'No file has been selected.','','0 of 0 Bytes');"
                scriptManager.RegisterStartupScript(Me, GetType(FileUploadFotosPDF), "progress", js, True)

Aucun commentaire:

Enregistrer un commentaire