Asp.net Code for Video File Upload in Server

How to upload files in ASP.Internet

Nowadays you may find file upload in near every website – from forums that permit users to upload photos for their avatars, to online auctions where users tin create galleries with a lot of images. The principal thing is that file upload should exist fast, like shooting fish in a barrel, and reliable.

There are a number of different upload approaches in ASP.NET. Thus, it is essential to cull the one that suits your requirements best: whether it is uploading a few files one by one or multiple files at a time, working with files of minor or very large size, sending entire folders or files only, having a simple image upload or preprocessing images beforehand.

In this article we consider dissimilar ways of file upload in ASP.Internet and discuss their use, only earlier let's see how file upload works in general.

Files Upload Basics

The upload procedure is quite elementary. There are ever two parts, the client and server sides, that communicate with each other through HTTP requests and responses. Let's consider the usual upload scenario:

  1. A user visits a web page and chooses files to upload.
  2. The client application packs these files to a POST request and sends it to the server.
  3. The server parses the request, handles information technology (for instance, saves files on a hard disk drive), and sends a response to the client side.

Y'all tin can pattern a client side using different components: ASP.Net or HTML controls, HTML5, Flash, Java, or ready-to-utilize upload applications. We will discuss them below.

Now allow's examine how the server works.

When the HTTP request is received, ASP.NET starts the uploading process: information technology caches all data in server memory or to deejay depending on the uploaded file size.

After all files are uploaded, the server code runs. Hither is where you tin handle uploaded data, for case: save the uploaded files to the appropriate location, examine their characteristics, update a database, etc.

Using ASP.Internet you lot don't need to parse the HTTP request manually, you just refer to the uploaded files through the HttpPostedFile collection. This makes life easier, considering all y'all demand is to iterate through each file in this drove and perform the necessary actions.

When the server lawmaking execution is finished, the server cleans upwardly the memory and sends an HTTP response to the client.

Y'all can configure your ASP.Internet application past editing the web.config (or machine.config if you lot want to change settings globally, for all ASP.Net applications on your server). At that place are several attributes in the <httpRuntime> department which are important for the file upload:

  • maxRequestLength – the request size limit in kilobytes (the default value is 4096 KB).
  • requestLengthDiskThreshold – the limit of data buffered in the server retentiveness in kilobytes (the default value is 80 KB).
  • executionTimeout – the allowed execution time for the request before being automatically shut down past ASP.NET (the default value is 110 seconds).

Notation: it is not recommended specifying very large (nigh unlimited) values as it may lead to the hazard of DoS attacks.

We've figured out the files upload system in ASP.NET, now it is time to examine different upload approaches. Permit's starting time with the simplest one – using the standard ASP.NET FileUpload control.

Using FileUpload Control

FileUpload supports single and multiple file uploads. It allows a user to choose a file to exist uploaded via the Browse button. The command doesn't automatically salve a selected file to the server, only it exposes the SaveAs method to perform this. Deploying FileUpload in your web application is very piece of cake. Firstly, let'due south talk over how to practise information technology for uploading a single file at a time.

Single File Upload

To add unmarried file upload functionality in your website but embed the FileUpload control to the <course> tag in the place where you want users to display the upload interface. The code may look every bit follows:

<html>                   <caput>         <title>Upload Files</title>     </head> <trunk>     <course id="form1" runat="server">         <asp:FileUpload ID="FileUpload1" runat="server" />         <br/>         <asp:Button ID="UploadButton" runat="server"                      OnClick="UploadButton_Click"                     Text="Upload File" />         <br/>         <asp:Label ID="FileUploadedLabel" runat="server" />     </form> </body> </html>

Later running this page, you volition encounter the following interface:

Simple file upload form in ASP.NET

Let's highlight the hard parts:

  1. The FileUpload control (like any other server control) needs to be included in the <form> tag.
  2. The form should have the runat="server" aspect, which indicates that the form is processed on the server and the FileUpload control tin be accessed by server scripts.
  3. The class should also contain id, proper name, and method attributes. Typically they are automatically generated by ASP.NET.
  4. The onClick attribute of the Upload File button specifies the event handler that processes file upload.

Later a user clicks the Upload File button, the form data will be sent to the server. The code of the Upload File push click handler should look like this:

protected void UploadButton_Click(object sender, EventArgs due east) {     if (FileUpload1.HasFile)                     endeavor         {             FileUpload1.SaveAs(Server.MapPath("~/uploads/") +                  FileUpload1.FileName);             FileUploadedLabel.Text = "File name: " +                  FileUpload1.PostedFile.FileName + "<br>" +                  FileUpload1.PostedFile.ContentLength + " kb<br>" +                  "Content type: " + FileUpload1.PostedFile.ContentType;         }         catch (Exception ex)         {             FileUploadedLabel.Text = "ERROR: " + ex.Message.ToString();         }     else     {         FileUploadedLabel.Text = "You have non specified a file.";     } }

This upshot handler checks if any file has been specified, tries to salvage it to the uploads binder, and displays a message indicating whether the file has been saved successfully. Note that the FileUpload1 proper name is similar to the FileUpload id aspect in the client form discussed higher up.

When adding uncomplicated upload to yous web application do not forget that information technology does non protect your server from the malicious files that a user tin upload. There are several security concerns which can aid yous to consider whether accept an uploaded file. For example, yous tin control the type of uploaded file by checking the extension (which can be easily spoofed) or the correct "magic number" in the file header.

To upload a single file at a time is very easy, merely it in rare utilize present. If you are going to design your ain site, most likely you lot volition need to upload several files at a time. And so, let'due south see how you tin practice it.

Multiple Files Upload

The most important advantage of the FileUpload server control is the back up of multiple file upload. To enable information technology, just slightly alter the customer application considered above. However, you should sympathize that the multiple upload feature works correctly only in browsers that support HTML5.

The merely modify required in the client awarding considered in a higher place is adding the AllowMultiple property which specifies whether multiple files can exist selected for upload:

<html> <head>     <title>Upload Files</title> </head> <body>     <grade id="form1" runat="server">         <asp:FileUpload ID="FilesUpload" runat="server"                          AllowMultiple="true" />         <br/>         <asp:Push button ID="UploadButton" runat="server"                      OnClick="UploadButton_Click"                     Text="Upload File" />         <br/>         <asp:Label ID="FileUploadedList" runat="server"/>     </form> </torso> </html>

The code behind the Upload File button also needs to be modified: before saving a file, the UploadButton_Click effect handler should iterate through all uploaded information. Now the handler code may look equally follows:

protected void UploadButton_Click(object sender, EventArgs e) {     if (FilesUpload.HasFile)         foreach (HttpPostedFile uploadedFile in FilesUpload.PostedFiles)             attempt             {                 uploadedFile.SaveAs(Server.MapPath("~/uploads/") +                                      uploadedFile.FileName);                 FileUploadedList.Text += "File name: " +                    uploadedFile.FileName + "<br>" +                    uploadedFile.ContentLength + " kb<br>" +                    "Content type: " + uploadedFile.ContentType + "<br><br>";             }             catch (Exception ex)             {                 FileUploadedList.Text = "Fault: " + ex.Message.ToString();             }     else     {         FileUploadedList.Text = "You take not specified a file.";     } }

As a event, you will get the same upload interface, only the text box displays the number of selected files:

Multiple file upload form for ASP.NET

The FileUpload control is great when you merely upload a few minor files, but it has several disadvantages:

  1. There is no opportunity to display uploading progress.
  2. Uploading a large number of files is very inconvenient, because the page does non respond while the file upload is processed.
  3. If a browser does not support HTML5, information technology won't piece of work at all.
  4. Every browser displays it in a different fashion.

Fortunately, some tertiary party uploaders are partially free of these concerns. Let'due south discuss them.

Tertiary Political party File Uploaders

Present all popular browsers back up the Flash or newer HTML5 platform, which allows the creating of advanced file upload using Flex or JavaScript respectively. Thus, there are a lot of 3rd-party uploaders. The almost popular are open up source HTML5/Flash-based uploaders, for example:

  • Uploadify is a jQuery plugin with a queue of files that are not uploaded yet, a real-time progress bar for each file, custom upload limitation, etc.
  • Fine Uploader is a JavaScript plugin tool with multiple file selection, progress bar, car and manual upload, image preview, etc.
  • Plupload is an upload tool based on several platforms, which even includes some paradigm processing functionality, etc.

These uploaders are very good at multiple file upload, provide a simple interface, and are supported by a large community of developers. However, the following tasks cannot be solved by these uploaders:

  • Preprocessing uploaded images (resize, crop, rotate, generate thumbnails, add watermarks, etc.).
  • Uploading entire folders and keeping a binder's construction on the server.
  • Uploading a large corporeality of files.
  • Big files upload (of hundreds MB or even several GB).
  • Automatic restore of the cleaved uploads.
  • Speeding up the upload process.

All these scenarios are ideal for Aurigma's Upload Suite. Yous can do it with few lines of lawmaking.

Come across how to get started

Become a free 30-day trial

Upload Suite includes premium uploaders based on various technologies (HTML5, Flash, Java, ActiveX) for any server engineering science – ASP.NET and PHP, classic ASP and JSP, Ruby-on-rails and node.js.

prenzelpubset.blogspot.com

Source: https://www.aurigma.com/upload-suite/developers/aspnet/how-to-upload-files-in-aspnet

0 Response to "Asp.net Code for Video File Upload in Server"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel