File Upload in ASP.NET Ivaylo Kenov Technical Assistant

Slides:



Advertisements
Similar presentations
PHP File Upload ISYS 475.
Advertisements

Video, audio, embed, iframe, HTML Form
FILE UPLOAD.
Multiple Tiers in Action
Course Content, Evaluation, Exams
Data-tier Application, Import, Refactoring, Publish, Schema Comparison, Database Unit Testing Borislav Statev Telerik Software Academy academy.telerik.com.
Course Content, Evaluation, Exams Telerik Software Academy ASP.NET Web Forms.
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
.Net is a collection of libraries, templates and services designed to make programming applications of all kinds, easier, more flexible (multi platform),
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Website Development with PHP and MySQL Saving Data.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
Telerik Software Academy ASP.NET Web Forms Telerik Software Academy ASP.NET Web Forms.
Telerik Software Academy ASP.NET Web Forms.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
CST336, Spring 2015 Week 8: PHP File Upload. PHP provides specific functions to handle binary data such as uploading a file into the server, storing it.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
SOAP-based Web Services Telerik Software Academy Software Quality Assurance.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
1 HTML forms (cont.)
ICM – API Server & Forms Gary Ratcliffe.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
HTML Forms.
New Generation University Faculty of Computer Science Chapter Five: File Uploaded and Ad Rotate Lecturer: Mukhtar Mohamed Ali “Hakaale”
1111 Creating HTML Programatically Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML.
Saving Files to Database Sams ASP NET 4 Unleashed.
Craig Pelkie Copyright © 2015, Craig Pelkie ALL RIGHTS RESERVED Use RPG to Mobilize your IBM i.
FILE UPLOAD.
Introduction to ASP.NET development. Background ASP released in 1996 ASP supported for a minimum 10 years from Windows 8 release ASP.Net 1.0 released.
JavaScript and Ajax Week 10 Web site:
CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
Kendo UI ASP.NET MVC Wrappers
JS Frameworks Course Program, Evaluation, Exams
Static Members and Namespaces
Site Maps and Navigation
NoSQL Databases NoSQL Concepts Databases Telerik Software Academy
Redis and Redis with .NET
Done already for your convenience!
Setup QA Process Software Quality Assurance Telerik Software Academy
ASP.NET AJAX – Basics Svetlin Nakov Telerik Corporation
Google APIs and Facebook API
Basic Math in 2D Game Development
Repeating Code Multiple Times
Unleash the Power of jQuery
Allowing File Uploads.
Using ASP.NET Master Pages
2D Graphics and Animations in Unity 3D
Entity Framework Performance
CS520 Web Programming Bits and Pieces of Web Programming (II)
AJAX and jQuery AJAX AJAX Concepts, XMLHttpRequest, jQuery AJAX: $.ajax(), $.get(), $.post() jQuery AJAX XMLHttpRequest SoftUni Team Technical Trainers.
Kendo UI ASP.NET MVC Wrappers
Multidimensional Arrays
Web Service Testing …in another way Software Quality Assurance
CS3220 Web and Internet Programming Generating HTTP Responses
Data Definition and Data Types
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Exporting and Importing Data
Binary, Decimal and Hexadecimal Numbers
Software Quality Assurance
Subroutines in Computer Programming
File Types, Using Streams, Manipulating Files
Allowing File Uploads.
Presentation transcript:

File Upload in ASP.NET Ivaylo Kenov Technical Assistant Telerik Software Academy academy.telerik.com

Table of Contents File Upload Concepts Multipart HTTP Requests File Upload in ASP.NET Web Forms To server file To memory stream Using Kendo UI Upload Asynchronous Large files

* How files are sent through the web File Upload Concepts How files are sent through the web (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

File Upload Concepts Done through forms Content type should be “multipart/form-data” Selected via input type “file” tag Data is sent as POST method through http Files are received and used at the server <form id="FileUploadForm" runat="server"> <asp:FileUpload ID="FileControl" runat="server" /> <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClick="Upload" /> </form>

Multipart HTTP Request * Multipart HTTP Request Different kind of POST method (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Multipart HTTP Request Form is sent as multiple parts Enable multipart – type “multipart/form=data” Request is divided in parts Content-Type: multipart/form-data; boundary=---------------------------7d81b516112482 Content-Length: 324 -----------------------------7d81b516112482 Content-Disposition: form-data; name="file"; filename=“Exam solution.doc" Content-Type: text/plain SomeText Content-Disposition: form-data; name="submit"

Multipart HTTP Request Live Demo

ASP.NET Web Forms File Upload * ASP.NET Web Forms File Upload Done within minutes (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

ASP.NET File Upload Classes Request.Files Gets the collection of sent files (dictionary) Uses MIME format HttpPostedFile Provides access to individual files string FileName property int ContentLength in bytes InputStream – stream of the file SaveAs(string fileName) – saves the file

ASP.NET File Upload Classes bool HasFile – checks if file is uploaded byte[] FileBytes – file content in binary Stream FileContent – file as stream string FileName – name of the file HttpPostedFile PostedFile – posted file IList<HttpPostedFile> PostedFiles – collection of all uploaded files

ASP.NET File Upload Using <asp: FileUpload /> In the code behind <asp:FileUpload ID="FileControl" runat="server" /> if (FileControl.HasFile) { string filename = Path.GetFileName (FileControl.FileName); FileControl.SaveAs(Server.MapPath ("~/Uploaded_Files/") + filename); }

Simple File Upload Live Demo

Restricted File Upload You can check for certain file types You can check for file size if (FileControl.PostedFile.ContentType == "image/jpeg") { // Use file } else // do not upload if (FileControl.PostedFile.ContentLength < 102400) { // Use file } …

Restricted File Upload Live Demo

Uploading To Stream Saving file to stream byte[] fileData = null; Stream fileStream = null; int length = 0; if (FileUploadControl.HasFile) { length = FileControl.PostedFile.ContentLength; fileData = new byte[length + 1]; fileStream = FileControl.PostedFile.InputStream; fileStream.Read(fileData, 0, length); } MemoryStream stream = new MemoryStream(fileData);

Uploading To Stream Live Demo

* Asynchronous and large files support Kendo UI Upload Widget Asynchronous and large files support (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Kendo UI Upload Kendo Upload widget Easily done Have upload and delete Styled and ready to use Supports asynchronous file upload Supports multiple file upload at once Supports sending large files in parts

Kendo UI Upload Steps for asynchronous large multi-upload Create landing page for the widget Add input type “file” (no form required) Add JavaScript for the widget Add upload handler Add remove handler (if you need it) In web.config set request max length <httpRuntime targetFramework="4.5" executionTimeout="110" maxRequestLength="25000" />

Kendo UI Upload Landing page <input name="uploaded" id="uploaded" type="file" runat="server" /> <script> $(document).ready(function () { $("#uploaded").kendoUpload({ async: { saveUrl: "Upload", removeUrl: "Remove", autoUpload: true, } }); </script>

Kendo UI Upload Upload page Get name of the input from Request.Files Return empty response if successful Response.Expires = -1; HttpPostedFile file = Request.Files["uploaded"]; string savepath = Server.MapPath("~/Uploaded_Files/"); string filename = file.FileName; file.SaveAs(savepath + filename); Response.ContentType = "application/json"; Response.Write("{}");

Kendo UI Upload Remove page Get file from Request.Form[“fileNames”] If file exists – delete it var file = Request.Form["fileNames"]; if (file != null) { var fileName = Path.GetFileName(file); var physicalPath = Path.Combine (Server.MapPath("~/Uploaded_Files"), fileName); if (File.Exists(physicalPath)) File.Delete(physicalPath); }

Kendo UI Upload Widget Live Demo

ASP.NET Identity http://academy.telerik.com

Homework Create a Web Forms upload page using Kendo UI for archive with text files. The upload must work only with .zip file extension. You should not write the zip file into the server file system. Save the file to the memory and using external library, extract the text files and save their content into the database. The text files should not be saved to the file system either.

Free Trainings @ Telerik Academy C# Programming @ Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com