This relates to the new resources section I am developing for TBH but also to the File Upload control in general.
You can read about my planned update here
As I am running out of time I am currently using the TBH fileupload.ascx control to upload the resource files and then copying the path in (like with category image url's).
I have had to do this until so that I can get some data uploaded in order to complete the downloads section for the showarticle.aspx page.
[u]What I want to achieve</u>
On my AddEditResource.aspx page, in place of the current fileupload control i want a standard <asp:FileUpload.../> control. Ideally I would like this within the item template of my Resources detail view where the FileName field would typically show (although when I tried I could not then reference the control programatically).
So when the user adds a new resource, he completes the title and description fields of the record. Then he clicks the browse button on the fileupload control and selects a file.
When the user clicks insert, the file is uploaded and the filename (not the full path) is stored is passed to my InsertResource method.
Also if the user changes the file for an existing resource record, the original file is deleted.
The code for uploading the file and deleting the existing is fine. If you could answer these questions:
1). If my fileupload control is within my DetailsView control template, how do I access it programmatically? Do I use the findcontrol method?
2). My InsertResource method expects a value @FileName. Currently I am using an InsertParameter within my ObjectDataSource which looks at the FileName item within the DetailsView. How can I change this so that it passes the name of the file that was uploaded? Im not sure whether I can use a control paramater to look at the FileUpload control and reference it's FileName property (especially if its inside by details view)
3). The idea would be that the Insert button will upload the file and pass the filename paramater to the InsertResource method. What event should I use to execute this code. I tried "Inserting" but this failed.
Finally something other people may want to implement.
For me the way that the fileuploader control works didnt really suit my needs. Rather than always uploading to uploads/currentuser I wanted all my uploads to go into one folder unless
I think for a site that has a large number of contributors, naming the folder using the username is a good idea, but for my client this didnt work.
Also I wanted the upload directory to be easily configurable. Currently fields such as ImageUrl for category have the full site path stored. This could cause problems if you ever move your site (i.e.your development site may be called something different to your live site).
My solution was to do the following:
1) I created a new config section element:
2) Added this property to my ConfigSection class so that the directory name would be accessible through:
3) Added public property FolderName to the FileUpload.ascx control. I use some logic to check whether a folder name is passed. If not the private _folderName variable is set to "".
4) I changed the code executed when the upload button is clicked to the below:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If Not IsNothing(filUpload.PostedFile) AndAlso filUpload.PostedFile.ContentLength > 0 Then
'get resources foldername from web.config
Dim resourceFolder As String = Globals.Settings.Resources.ResourcesDirectory
' if not already present, create the directory
Dim dirUrl As String = CType(Me.Page, Retro.Web.UI.BasePage).BaseUrl + _
resourceFolder + "/" + FolderName
'check if folder specified
Dim dirPath As String = Server.MapPath(dirUrl)
If Not Directory.Exists(dirPath) Then
'get just the name of the uploaded file
Dim fileName As String = Path.GetFileName(filUpload.PostedFile.FileName)
Dim fileUrl As String = dirUrl + fileName
lblFeedbackOK.Visible = True
lblFeedbackOK.Text = "File successfully uploaded: " + fileName
Catch ex As Exception
lblFeedbackKO.Visible = True
lblFeedbackKO.Text = ex.Message
Then in places where you need to get the full path (i.e. ImageUrl) just append the baseurl to your navigateUrl property.
I can now set the FileName property on my control on the Category page to "Images" and the one on my resources page to "ftp". If dont specify a foldername the file will be uploaded to the root of the directory specified in your config file (in my case "resource")