How to upload a file in a specific directory 
January 14th, 2009, 08:26 AM
| | Member | | Join Date: Sep 2008
Posts: 74
| | |
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.
| 
January 14th, 2009, 12:45 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
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.
| 
January 14th, 2009, 01:27 PM
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,569
| | | re: How to upload a file in a specific directory
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.
| 
January 14th, 2009, 01:52 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
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??
| 
January 14th, 2009, 02:04 PM
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,569
| | | re: How to upload a file in a specific directory
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.
| 
January 14th, 2009, 06:43 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
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. -
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
-
-
| 
January 14th, 2009, 07:50 PM
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North :)
Posts: 4,940
Provided Answers: 8 | | | re: How to upload a file in a specific directory
You are probably getting the error because the row.RowIndex is larger than your gvFolderItems.DataKeys.
Before you do the following: - Dim RowID As Integer = gvFolderItems.DataKeys(row.RowIndex).Value
You should check that the row.RowIndex value is within limits.
| 
January 14th, 2009, 07:52 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
How do I do that? Can you please give me some hint on the code.thanks.
| 
January 14th, 2009, 08:04 PM
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North :)
Posts: 4,940
Provided Answers: 8 | | | re: How to upload a file in a specific directory
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.
| 
January 14th, 2009, 08:08 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
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.
| 
January 14th, 2009, 08:18 PM
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North :)
Posts: 4,940
Provided Answers: 8 | | | re: How to upload a file in a specific directory
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 -
-
MyTreeView | MyGridView w/in a scrollable Panel
-
|
-
|
-
| AddFileButton AddFolderButton DeleteFileOrFolderButton
Or I'd place the buttons on top of the GridView -
MyTreeView | AddFileButton AddFolderButton DeleteFileOrFolderButton
-
| MyGridView w/in a scrollable Panel
-
|
-
|
| 
January 14th, 2009, 08:21 PM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
sorry about that. Yes I have the exact settings in my page.
| 
January 15th, 2009, 09:28 AM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory 
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
| 
January 15th, 2009, 02:14 PM
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North :)
Posts: 4,940
Provided Answers: 8 | | | re: How to upload a file in a specific directory
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. - '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
-
| 
January 19th, 2009, 07:07 AM
| | Member | | Join Date: Sep 2008
Posts: 74
| | | re: How to upload a file in a specific directory
Thxs Frinavale for your suggestions & help, finally I have developed the system.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|