How to upload a file in a specific directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kashif73
    New Member
    • Sep 2008
    • 91

    How to upload a file in a specific directory

    Hi Everyone,
    can anyone please let me know how to upload a file to a specific directory. I am using ASP.NET Fileupload component. I want to allow user to upload files to a directory or a sub directory. Depending upon their choice, they can upload in any folder. How can I trace in my web application that a user is in a particular folder & the document he uploads get uploaded in the correct folder or sub folder??? Thanks.
  • kashif73
    New Member
    • Sep 2008
    • 91

    #2
    Actually I want to create FTP like application, where users can make, delete, rename directories & subsequently upload files in any of the directory. Any hint or help will much be appreciated. Thanks.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Consider using a TreeView control to show your directory hierarchy. You could add some buttons to add a child directory to the selected node. And you could have a FileUpload control that will save the uploaded file to the selected directory.

      If you modify this a little bit to show files, you could have a button to delete a selected file.

      This isn't going to be a simple project, but it shouldn't be too hard either.

      Comment

      • kashif73
        New Member
        • Sep 2008
        • 91

        #4
        Thanks InsertAlias for your reply. I have put a TreeView control to show my main directory structure. I show the files of each directory in a GridView infront of it. However I am unable to understand, where do I put the button which create directory in the selected node and also upload files in it?? Is there a way that I can add a button in TreeView or GridView and put FileUpload as well??

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          What I would suggest is adding either a CheckBoxField or a TemplateField with a Checkbox in it to your GridView. Then have a button outside the GridView that says something like "Delete All Selected Files" or something like that.

          Then loop through the gridview and delete all the files that are checked.

          As for the other buttons, I'd keep them outside the gridview/treeview. You can put the button to create a new directory below your treeview or something like that. And keep the FileUpload outside the gridview too.

          Comment

          • kashif73
            New Member
            • Sep 2008
            • 91

            #6
            InsertAlias, As suggested I have put a checkbox inside GridView with delete button outside, to delete all checked entries. But Itis throwing index out of range error..dont know why? unable to delete checked files.


            Code:
            Protected Sub DeleteSelected_Click(ByVal sender As Object, ByVal e As EventArgs) _
                        Handles DeleteSelected.Click
                        For Each row As GridViewRow In gvFolderItems.Rows
                            ' Access the CheckBox
                            Dim cb As CheckBox = row.FindControl("ProductSelector")
                            If cb IsNot Nothing AndAlso cb.Checked Then
                                ' Delete row
                                Dim RowID As Integer = gvFolderItems.DataKeys(row.RowIndex).Value
                                atLeastOneRowDeleted = True
                                gvFolderItems.DeleteRow(RowID)
                            End If
                        Next
                    End Sub

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              You are probably getting the error because the row.RowIndex is larger than your gvFolderItems.D ataKeys.

              Before you do the following:
              Code:
              Dim RowID As Integer = gvFolderItems.DataKeys(row.RowIndex).Value
              You should check that the row.RowIndex value is within limits.

              Comment

              • kashif73
                New Member
                • Sep 2008
                • 91

                #8
                How do I do that? Can you please give me some hint on the code.thanks.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Just to recap:

                  You've created a TreeView that shows your file hierarchy on the left side of the page.

                  When a user clicks on a folder in the TreeView your application displays the files and folders found in that selected folder in a GridView.

                  Now since you want to let the user add and delete files you've put buttons under the GridView.

                  To delete files or folders, you have the user place check marks next to the things they want to delete.

                  When the click the Delete button your code should loop through and delete the files checked.


                  Your code does not currently do this.
                  First you should loop through your GridView's rows and check which ones have been selected. If you find a selected item, you should store the path to the selected item in an ArrayList or List(Of String). Then pass this to a new function that deletes the files/folders in that list.

                  After you have finished this process you should recreate the source for the GridView to display the current contents of the file selected.

                  Comment

                  • kashif73
                    New Member
                    • Sep 2008
                    • 91

                    #10
                    Yes right, I have put checkbox in GridView so that they can select files to delete. The DELETE SELECTED FILES button is outside of GridView.

                    One more thing boss, how should I put a button which adds a sub-folder in a selected folder , basing on the user selection (selection from the treeview)??

                    Thanks.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      You're trying to do too many things at once here. Address one problem and then move to the next or we're going to get confused.

                      Anyways I'd lay out the screen

                      Code:
                      MyTreeView       |      MyGridView w/in a scrollable Panel
                                       |  
                                       |
                                       |    AddFileButton      AddFolderButton       DeleteFileOrFolderButton
                      Or I'd place the buttons on top of the GridView


                      Code:
                      MyTreeView       |    AddFileButton      AddFolderButton       DeleteFileOrFolderButton
                                       |      MyGridView w/in a scrollable Panel
                                       |  
                                       |

                      Comment

                      • kashif73
                        New Member
                        • Sep 2008
                        • 91

                        #12
                        sorry about that. Yes I have the exact settings in my page.

                        Comment

                        • kashif73
                          New Member
                          • Sep 2008
                          • 91

                          #13


                          This is the screenshot of what i have done so far. But I am still unable to configure the DELETE SELECTED FILES or FOLDERS from my gridview. Please anyone can help here

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            You need to retrieve the names of the files that are selected in the GridView.

                            Once you have the names you need to loop through them and delete the files.

                            After you're finished with that you need to recreate the DataSource for the GridView so that it displays the current files in the direcotry.

                            Code:
                            'Will contain the names of all the files to delete
                            Dim Dim fileNamesToDelete As New List(Of String)
                            
                            For Each row As GridViewRow In gvFolderItems.Rows
                               'Retrieve the CheckBox for the row
                                Dim cb As CheckBox = row.FindControl("ProductSelector")
                             
                               'Check if the row has been selected
                               If cb IsNot Nothing AndAlso cb.Checked Then
                                  Dim fileName As String = CStr(row.Cells(0).Value)'<--should be which ever cell contains the file name
                                  fileNamesToDelete.Add(fileName)
                               Next
                            
                               'Now loop through the file names to delete and delete them
                               'After you're done this, refresh the GridView's DataSource

                            Comment

                            • kashif73
                              New Member
                              • Sep 2008
                              • 91

                              #15
                              Thxs Frinavale for your suggestions & help, finally I have developed the system.

                              Comment

                              Working...