Allowing File Uploads.

Slides:



Advertisements
Similar presentations
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
Advertisements

Python and Web Programming
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Creating Web Page Forms
11 ASP.NET Controls Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
1 ADVANCED MICROSOFT WORD Lesson 15 – Creating Forms and Working with Web Documents Microsoft Office 2003: Advanced.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
JavaScript & jQuery the missing manual Chapter 11
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
© 2008 Dr. Paul Walcott – The University of the West Indies: Cave Hill CampusDr. Paul Walcott COMP6325 Advanced Web Technologies Dr. Paul Walcott The University.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
1 HTML Forms. 22 HTML forms provide a way for a user to send information back to the web server. Originally the user input was processed on the server.
INSPIRING CREATIVE AND INNOVATIVE MINDS Module 4: Adding Code to a Microsoft ASP.NET Web Form Implementing Code-Behind Pages Adding Event Procedures to.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
Copyright © 2004 ProsoftTraining, All Rights Reserved. Lesson 7: Web Forms © 2007 Prosoft Learning Corporation All rights reserved ITD 110 Web Page Design.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
Active Server Pages  In this chapter, you will learn:  How browsers and servers interacted on the Internet when the Internet first became popular 
Website Development with PHP and MySQL Saving Data.
Introduction to ASP.NET T.Ahlam Algharasi. The Visual Studio IDE Start page 2.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Unit 3: Adding Code to a Microsoft ASP.NET Web Form.
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
Microsoft FrontPage 2003 Illustrated Complete Creating a Form.
Introduction to JavaScript CS101 Introduction to Computing.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
New Generation University Faculty of Computer Science Chapter Five: File Uploaded and Ad Rotate Lecturer: Mukhtar Mohamed Ali “Hakaale”
11 ASP.NET Server Controls Beginning ASP.NET in C# and VB Chapter 4.
1 HTML Forms. Objectives You will be able to: Compose HTML forms, to accept input from the user. Identify the different kinds of input tags that can appear.
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Beginning ASP.NET in C# and VB Chapter 9
Working with ASP.NET Controls What is ASP.NET Using server controls in your pages Allowing users to create their own accounts Creating a login page Letting.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Creating Consistent Looking Websites
Section 10.1 Define scripting
Computing with C# and the .NET Framework
ASP.NET Forms.
Working with ASP.NET Server Controls
ITEC 420 Framework Based Internet Applications
Session Variables and Post Back
Chapter 8 User Controls.
Using the HTML and CSS Validation Services
How to Create a Simple Web Service and Use it in ASP.Net
File Upload in ASP.NET Ivaylo Kenov Technical Assistant
Client side & Server side scripting
Module 10: Creating a Web Application with Web Forms
ASP.NET.
Lecture 5: Functions and Parameters
5/6/2019 Session 8.2 Postback, ViewState
ITEC 420 Framework Based Internet Applications
Allowing File Uploads.
Presentation transcript:

Allowing File Uploads

The File Upload Control Fortunately, ASP.NET includes a control that allows website users to upload files to the web server. Once the web server receives the posted file data, it’s up to your application to examine it, ignore it, or save it to a back-end database or a file on the web server. The FileUpload control does this work, and it represents the <input type="file"> HTML tag. Declaring the FileUpload control is easy. It doesn’t expose any new properties or eventsyou can use through the control tag: <asp:FileUpload ID="Uploader" runat="server" />

The File Upload Control Contd. The <input type="file"> tag doesn’t give you much choice as far as user interface is concerned (it’s limited to a text box that contains a file name and a Browse button). When the user clicks Browse, the browser presents an Open dialog box and allows the user to choose a file. This part is hard-wired into the browser, and you can’t change this behavior. Once the user selects a file, the file name is filled into the corresponding text box. However, the file isn’t uploaded yet—that happens later, when the page is posted back. At this point, all the data from all input controls (including the file data) is sent to the server. For that reason, it’s common to add a button to post back the page. To get information about the posted file content, you can access the FileUpload.PostedFile object. You can save the content by calling the PostedFile.SaveAs() method: Uploader.PostedFile.SaveAs(@"c:\Uploads\newfile");

An example which accepts .bmp, .gif and .jpg extensions We have three components on the aspx page: A File Upload Control named Uploader. A Button Control named Upload. A Label Control named lblInfo.

An example which accepts .bmp, .gif and .jpg extensions public partial class UploadFile : System.Web.UI.Page { private string uploadDirectory; protected void Page_Load(object sender, EventArgs e) // Place files in a website subfolder named Uploads. uploadDirectory = Path.Combine( Request.PhysicalApplicationPath, "Uploads"); } protected void cmdUpload_Click(object sender, EventArgs e) // Check that a file is actually being submitted. if (Uploader.PostedFile.FileName == "") lblInfo.Text = "No file specified."; else // Check the extension. string extension = Path.GetExtension(Uploader.PostedFile.FileName);

An example which accepts .bmp, .gif and .jpg extensions switch (extension.ToLower()){ case ".bmp": case ".gif": case ".jpg": break; default: lblInfo.Text = "This file type is not allowed."; return; } // Using this code, the saved file will retain its original // file name when it's placed on the server. string serverFileName = Path.GetFileName( Uploader.PostedFile.FileName); string fullUploadPath = Path.Combine(uploadDirectory, serverFileName); try{ Uploader.PostedFile.SaveAs(fullUploadPath); lblInfo.Text = "File " + serverFileName; lblInfo.Text += " uploaded successfully to"; lblInfo.Text += fullUploadPath; catch (Exception err){ lblInfo.Text = err.Message;}}}}

Additional Notes Dissecting the Code . . . The saved file keeps its original (client-side) name. The code uses the Path.GetFileName() static method to transform the fully qualified name provided by FileUpload.Posted-File.FileName and retrieve just the file, without the path. The FileUpload.PostedFile object contains only a few properties. One interesting property is ContentLength, which returns the size of the file in bytes. You could examine this setting and use it to prevent a user from uploading excessively large files.

Limitations By default, ASP.NET will reject a request that’s larger than 4MB. However, you can alter this maximum by modifying the maxRequestLength setting in the web.config file. This sets the largest allowed file in kilobytes. The web server will refuse to process larger requests. The following sample setting configures the server to accept files up to 8MB: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <!-- Other settings omitted for clarity. --> <httpRuntime maxRequestLength="8192" /> </system.web> </configuration> Be careful, though. When you allow an 8MB upload, your code won’t run until that full request has been received. This means a malicious server could cripple your server by sending large request messages to your application. Even if your application ultimately rejects these messages, the ASP.NET worker process threads will still be tied up waiting for the requests to complete. This type of attack is called a denial-of-service attack, and the larger your allowed request size is, the more susceptible your website becomes.