Wrox Programmer Forums
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 27th, 2007, 09:40 AM
Authorized User
Join Date: Jan 2007
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default File Upload Control


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.

[u]Key questions</u>
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 specified otherwise.

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:
<resources directory="Resource"/>
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
                    End If
                    '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
                End Try
            End If
        End Sub

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")



Similar Threads
Thread Thread Starter Forum Replies Last Post
file upload control gaurav gupta ASP.NET 2.0 Professional 1 July 15th, 2008 06:45 AM
file upload control iamsunil2006 .NET Framework 2.0 1 January 21st, 2008 05:49 AM
File Upload Control surendraparashar ASP.NET 2.0 Professional 6 November 6th, 2007 07:39 AM
File upload control miltonsnider BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 December 12th, 2006 06:03 AM
file upload control brasi12345 ASP.NET 2.0 Professional 0 May 11th, 2006 05:47 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.