locked
Collecting upload files name to an array RRS feed

  • Question

  • User-489530237 posted

    Hello,

    I'm trying to collect a string (filename) after it get's renamed if file exsit on upload into an array.  No matter what I do I can't get it to display to a label.   Can someone let me know how I can approach the issue?  Been working over two weeks on the issue, your input would be a great help.

     

     

        Private Sub uploadfiles()
    
            Dim uploads As HttpFileCollection
            uploads = HttpContext.Current.Request.Files
    
            For i As Integer = 0 To (uploads.Count - 1)
                If (uploads(i).ContentLength > 0) Then
    
                    ' get the file instance 
                    Dim fi As HttpPostedFile = Request.Files.[Get](i)
    
                    ' create a byte array to store the file bytes 
                    Dim fileBytes As Byte() = New Byte(fi.ContentLength - 1) {}
    
                    ' fill the byte array 
                    Using stream As System.IO.Stream = fi.InputStream
                        stream.Read(fileBytes, 0, fi.ContentLength)
                    End Using
    
                    Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)
    
                    'Call a function to rename file if exsit
                    Dim newc As String = MakeFileName(c)
    
                    Try
                        'uploads(i).SaveAs("C:\UploadedUserFiles\" + newc)
                        File.WriteAllBytes("C:\UploadedUserFiles\" + newc, ResizeImageFile(fileBytes, 500))
    
                    Catch Exp As Exception
                        Span1.InnerHtml = "Some Error occured."
                    End Try
                End If
            Next i
        End Sub
    
        Function MakeFileName(ByVal FileName As String) As String
    
            Dim NewFileName As String = System.IO.Path.GetFileNameWithoutExtension(FileName)
            Dim Extension As String = System.IO.Path.GetExtension(FileName)
            Dim Increment As Integer
            Do Until System.IO.File.Exists("C:\UploadedUserFiles\" + FileName) = False
                Increment = Increment + 1
                FileName = (NewFileName & "_" & CStr(Increment) + Extension)
            Loop
            MakeFileName = FileName
    
        End Function


     

    Tuesday, June 22, 2010 7:56 PM

Answers

  • User-925286913 posted
    Function MakeFileName(ByVal FileName As String) As String
    
    	If Not System.IO.File.Exists("C:\UploadedUserFiles\" + FileName) Then
    		Return FileName
    	Else
    		Dim NewFileName As String = System.IO.Path.GetFileNameWithoutExtension(FileName)
    		Dim Extension As String = System.IO.Path.GetExtension(FileName)
    		Dim Increment As Integer = 1
    
    		While System.IO.File.Exists("C:\UploadedUserFiles\" + (NewFileName & "_" & CStr(Increment) + Extension))
    			Increment = Increment + 1
    		End While
    		Return (NewFileName & "_" & CStr(Increment) + Extension)
    	End If
    
    End Function
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 23, 2010 3:24 AM
  • User-925286913 posted

    I wanted to create a separate sub routine to collect all the renamed Filename into a separate array

    I don't understand it 100%.

    But, I think you want to get array of files with similar pattern. Like MyFile_1.jpg, MyFile_2.jpg, MyFile_3.jpg .....

    For that, you don't have to loop throught all the files.

    You can use Diretory.GetFiles method with pattern "FILENAME_*.jpg", where FILENAME is name of your file and * will substitute all numbers which are suffixed.

    Refer:

    http://msdn.microsoft.com/en-us/library/wz42302f.aspx

    http://blogs.msdn.com/b/markda/archive/2006/04/20/580075.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 24, 2010 1:32 AM

All replies

  • User-925286913 posted
    Function MakeFileName(ByVal FileName As String) As String
    
    	If Not System.IO.File.Exists("C:\UploadedUserFiles\" + FileName) Then
    		Return FileName
    	Else
    		Dim NewFileName As String = System.IO.Path.GetFileNameWithoutExtension(FileName)
    		Dim Extension As String = System.IO.Path.GetExtension(FileName)
    		Dim Increment As Integer = 1
    
    		While System.IO.File.Exists("C:\UploadedUserFiles\" + (NewFileName & "_" & CStr(Increment) + Extension))
    			Increment = Increment + 1
    		End While
    		Return (NewFileName & "_" & CStr(Increment) + Extension)
    	End If
    
    End Function
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 23, 2010 3:24 AM
  • User-489530237 posted

    Hello Ahmedabad Thanks for the quick response,

    I guess I should of made it more clearer.  I wanted to create a separate sub routine to collect all the renamed Filename into a separate array like the one below (GetFileNameArray).  The main goal is to create a session array to create a preview page.  Your help would be appreciated.

     

    Thx

     

        Private Sub uploadfiles()
    
            Dim uploads As HttpFileCollection
            uploads = HttpContext.Current.Request.Files
    
            For i As Integer = 0 To (uploads.Count - 1)
                If (uploads(i).ContentLength > 0) Then
    
                    ' get the file instance 
                    Dim fi As HttpPostedFile = Request.Files.[Get](i)
    
                    ' create a byte array to store the file bytes 
                    Dim fileBytes As Byte() = New Byte(fi.ContentLength - 1) {}
    
                    ' fill the byte array 
                    Using stream As System.IO.Stream = fi.InputStream
                        stream.Read(fileBytes, 0, fi.ContentLength).ToString()
                    End Using
    
                    Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName).ToString()
    
                    'Call a function to rename file if exsit
                    Dim newc As String = MakeFileName(c).ToString
                    GetFileNameArray(newc, i)
    
    
                    Try
                        'uploads(i).SaveAs("C:\UploadedUserFiles\" + newc)
                        File.WriteAllBytes("C:\UploadedUserFiles\" + newc, ResizeImageFile(fileBytes, 500))
    
                    Catch Exp As Exception
                        Span1.InnerHtml = "Some Error occured."
                    End Try
                End If
            Next i
        End Sub
    
        Function MakeFileName(ByVal FileName As String) As String
    
            If Not System.IO.File.Exists("C:\UploadedUserFiles\" + FileName) Then
                Return FileName
            Else
                Dim NewFileName As String = System.IO.Path.GetFileNameWithoutExtension(FileName)
                Dim Extension As String = System.IO.Path.GetExtension(FileName)
                Dim Increment As Integer = 1
    
                While System.IO.File.Exists("C:\UploadedUserFiles\" + (NewFileName & "_" & CStr(Increment) + Extension))
                    Increment = Increment + 1
                End While
                Return (NewFileName & "_" & CStr(Increment) + Extension)
            End If
    
        End Function
    
        Sub GetFileNameArray(ByVal filename As String, ByVal count As Integer)
            Dim mylist As ArrayList = New ArrayList()
            For i As Integer = 0 To count - 1
                mylist.Add(filename)
                Response.Write(mylist(i))
            Next
        End Sub


     

     

    Wednesday, June 23, 2010 2:32 PM
  • User-925286913 posted

    I wanted to create a separate sub routine to collect all the renamed Filename into a separate array

    I don't understand it 100%.

    But, I think you want to get array of files with similar pattern. Like MyFile_1.jpg, MyFile_2.jpg, MyFile_3.jpg .....

    For that, you don't have to loop throught all the files.

    You can use Diretory.GetFiles method with pattern "FILENAME_*.jpg", where FILENAME is name of your file and * will substitute all numbers which are suffixed.

    Refer:

    http://msdn.microsoft.com/en-us/library/wz42302f.aspx

    http://blogs.msdn.com/b/markda/archive/2006/04/20/580075.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 24, 2010 1:32 AM