Title: turton
1ASP.NETFile Upload, Exception Handling
- David Turton
- Conestoga College
- Institute of Technology and Advanced Learning
- http//www.conestogac.on.ca/dturton
- Doon 1D17, x 3610
2Objectives
- File Upload
- Upload files to a server
- Exception Handling
- Create exceptions
- Use Try-Catch-Finally construct
- To intercept exceptions (abends)
3File Upload
4File Upload
- Allows users to upload a file to server
- Browse button included
- To select local file to be uploaded
- You control destination on server
- User presents a file
- you code target path filename
- Individual user rights are not relevant
- It's ASPNET user doing the writing
5Text box for name of file at server (or default
to source file name)
Button to enact upload
Could provide a text box to input the target
directory or just default it by application, user
name, etc.
6Uploading Files to NTFSwith IIS user ASPNET
needs permissions
- Windows Explorer
- Tools ? Options
- "View" tab
- Clear Use Simple File Sharing (Recommended)"
- Security tab becomes available
- Gives access to NTFS permissions
- Give user ASPNET modify rights
- In folder properties, Security tab
7Allow ASPNET user to change files
properties
8CCode to enact upload
- Private void btnUpload_Click(ByVal sender As
-
- try
-
- if (txtServerName.Text null
txtServerName.Text.Trim() "") - lblError.Text "Error need file-name for
server" - else
-
- String uploadDir Server.MapPath("/images")
- upldSource.PostedFile.SaveAs(uploadDir _at_"\"
txtServerName.Text) -
-
- catch (Exception ex)
-
- lblMessage.Text "exception
thrown " ex.Message -
Get actual path to the site-relative path/images
9VBCode to enact upload
- Protected Sub btnUpload_Click(ByVal sender As
- Try
- If (txtServerName.Text Is Nothing OrElse
txtServerName.Text.Trim() "") - lblError.Text "Error need file-name for
server" - else
- Dim uploadDir As string Server.MapPath("/i
mages") - upldSource.PostedFile.SaveAs(uploadDir "\"
txtServerName.Text) - End If
- Catch ex As Exception
- lblMessage.Text "exception thrown
" ex.Message - End Try
Get actual path to the site-relative path/images
10Server.MapPath("/images")
- Returns actual physical path on server
- Starting from the drive letter
- Parameter is assumed to be a relative address
- Based on 1st character
- / means relative to the web site
- All else assumed relative to current web page
- "/" or "\" as a 1st character is invalid
- Will double "\" between path parameter
11Properties of Interestassume browse control is
called "upldSource"
- upldSource.PostedFile.FileName
- Returns source file-name and path
- UpldSource.FileName
- Returns source file-name extension, no path
- upldSource.PostedFile.ContentLength
- Length of file being uploaded check for zero
length - upldSource.PostedFile.ContentType
- Type of file being uploaded, as general/specific
pair image/jpg - File extension not always indicative of file type
(in Linux and UNIX) - System.IO.Path.GetFileName(upldSource.PostedFile.F
ileName) - Returns input file-name extension (no path)
- System.IO.Path.GetFileExtension(upldSource.PostedF
ile.FileName) - Returns input file-name extension only
- System.IO.Path.GetDirectoryName(upldSource
PostedFile.FileName) - Returns input file's source path
12Exception Handling
13Exception Handling (simple)
- For commands likely to get an exception (abend)
- Put within a Try construct
- Use Catch construct to interpret the error
- Usually, just display the error text ex.Message
- Exceptions outside a Try-Catch construct
- Will abend program kick user off your site
try commands to upload file, modify
database, etc. things to do if it works
catch (Exception ex) lblMessage.Text
"Here is the exceptionltbr /gt"
ex.Message finally things to do
regardless
14Try Catch - Finally
- Commands in Try are executed in sequence
- On an exception (error)
- Processing jumps to Catch code
- Remaining Try code is discarded
- Exception or not
- Commands in Finally block are executed
- Code following Try-Catch-Finally is executed
- Unless Try or Catch execute a Return
- (or Throw an exception back to calling program)
- Even if the code says to leave the procedure,
Finally is executed before it does so. - If you have a blank Catch block
- You're hiding the abend and processing will
continue - You'll lose marks on assignments
15MultiView Wizard Controls
16New with ASP.NET 3.5MultiView Wizard Controls
- Draft a single form
- Divide it into sections
- Present one section at a time
- Fields on all sections
- Available at all times
- MultiView
- You control
- .ActiveViewIndex
- Navigation buttons
- Content of each section
- Wizard
- Generates
- Previous/Next/Finish buttons
- Navigation menu
- You control
- Content of each section
17Assigning buttons to same handlerCreate one or
two button handlers, rename them (PanelNext,
PanelBack)Assign pertinent handler to all Next
and Back buttons
18MultiView ControlView-to-view navigation code
// on initial load, display the first
panel protected void Page_Load(object sender,
EventArgs e) if (!IsPostBack)
viewSurvey.ActiveViewIndex 0 // all next
buttons just increment the view index protected
void PanelNext(object sender, EventArgs
e) viewSurvey.ActiveViewIndex // all back
buttons decrement the view index protected void
PanelBack(object sender, EventArgs
e) viewSurvey.ActiveViewIndex--
19Wizard ControlMultiple-template formsYou
provide content ? it provides navigation
You provide content on each panel, the wizard
provides menu on left Next/Prior buttons
20Added by wizard
21ltaspWizardStepgt attributes(some)
- Title"xxx"
- Title of step shown in hyperlink navigation
- AllowReturn"False"
- If true (default), users can come back to this
step - If false, users can't, but can jump over it
- StepType"xxx"
- "Auto" default
- "Start" only Next button
- "Step" Next Previous
- "Finish" only Finish button
- "Complete" doesn't appear in hyperlink menu
22Wizard Events
- Xxx_NextButtonClick
- Look at e.NextStepIndex
- Next step to be displayed
- Can use input fields to decide whether to jump
this step or not
23Wizard Step event OnActivateuse to preload step
with data from other steps,decide whether to do
step, etc.
24(No Transcript)