Getting the Value and Display of Asp File Upload

ASP.NET-FileUpload

Introduction to ASP.NET FileUpload

A FileUpload server control is a command that displays a box and a browse button on the web page. It allows the user to select a file from the local storage and upload information technology to the server. Why did I use the term server control? Well, because like all other server controls, ASP.NET provides its own tag for the FileUpload control which is run at the server and the generated HTML lawmaking is returned equally a response to the browser. Then, thinking from an HTML perspective, the FileUpload control generates the HTML text box and a button that triggers the local file explorer. It lets the users select a file and upload it to the server.

Syntax:

The FileUpload control tin exist coded using ASP.Internet provided tags or dragged and dropped using Visual Studio IDE. The drag and driblet feature ultimately generates the same code.

<asp:FileUpload ID="<FileUploadId>" runat="server" />

Behind the Scenes

Then, how does the FileUpload machinery work? When the user selects a file from his local storage, the FileUpload command initiates an object which stores the basic information near the file. This information contains the file name, extension, directory path, file size, etc. The object has a property HasFile which is then gear up to true. This holding determines whether the user has selected any file or not.

Annotation that till this point, the file hasn't been uploaded to the server. Information technology has only been selected by the user. The browse button generated by the FileUpload command does not actually upload the file. This has to exist done by writing a piece of code that instructs the FileUpload control to salvage a copy of the file in a path on the server. When this piece of code is triggered, the FileUpload control simply sends the file over HTTP(S) protocol to the server and stores it at the specified path.

Backdrop of ASP.NET FileUpload

The ASP.Cyberspace FileUpload control comes with sure pre-defined properties. These properties are converted to attributes in the native HTML lawmaking. They help define additional beliefs for the FileUpload control.

1. AllowMultiple:This property gets or sets a value that permits or restricts the user to select multiple files simultaneously. The default value is imitation, which means the user is not allowed to select multiple files by default.

<asp:FileUpload ID="myFileUpload" AllowMultiple="truthful" runat="server" />

2. BackColor, ForeColor:This property gets or sets the groundwork and the foreground color of the control.

<asp:FileUpload ID="myFileUpload" BackColor="DarkBlue" ForeColor="White" runat="server" />

3. BorderColor, BorderStyle and BorderWidth: These properties become or set the edge styling for the control.

<asp:FileUpload ID="myFileUpload" BorderWidth="5" BorderColor="Blue" BorderStyle="dashed" runat="server" />

4. CssClass:This property gets or sets the CSS class to be applied to the command.

<asp:FileUpload ID="myFileUpload" CssClass="txtBxClass" runat="server" />

five. Enabled: This property gets or sets the value indicating whether the control is enabled or disabled. The default value is true.

<asp:FileUpload ID="myFileUpload" Enabled="false" runat="server" />

half-dozen. Font: This property gets or sets the font of the text to be displayed in the control. There are plenty of styles and options such as bold, italics, underline, strikeout, etc.

seven. Height, Width:These properties get or set the acme and width of the command in the number of pixels.

<asp:FileUpload ID="myFileUpload" Height="100" Width="500" runat="server" />

eight. ID:This property gets or sets the unique identifier aspect to the control.

<asp:FileUpload ID="myFileUpload" runat="server" />

9. ToolTip:This belongings gets or sets the tooltip value to exist displayed when the mouse pointer is hovered over the control.

<asp:FileUpload ID="myFileUpload" ToolTip="Click hither to select a file." runat="server" />

x. Visible:This property determines whether the control will be displayed on the UI or subconscious. The default is truthful.

<asp:FileUpload ID="myFileUpload" Visible="false" runat="server" />

Example of ASP.NET FileUpload

Permit us create a file upload sample application step by pace.

Step 1:Create a new ASP.Net WebApplication project. This volition create a beat template with a working application with a Default.aspx and Default.aspx.cs page. The .cs folio is the code behind page for the .aspx page.

Footstep ii:Go to the Default.aspx file and remove the contents of the shell template to await like below:

asp.net fileupload 1

Step iii. In the Toolbox pane of Visual Studio IDE, you would notice a prepare of Web controls set to elevate and drop in your project. Find the FileUpload control and drag it in the Default.aspx page.

asp.net fileupload 2

Step 4:Once you drop the FileUpload control, you would detect an auto-generated ASP.Internet FileUpload tag in your Default.aspx file. Modify the code to look like beneath.

asp.net fileupload 3

Alternatively, you can skip the drag and drop part and write the above code yourself.

<asp:FileUpload ID="FileUpload_Control" runat="server" />

Footstep 5: Copy the beneath code in your Default.aspx file.

<%@ Page Title="Abode Page" Linguistic communication="C#" MasterPageFile="~/Site.Primary"
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SampleWebApplication._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Characterization ID="FileUpload_Caption" runat="server" Text="Choose a file to upload."></asp:Label>
<asp:FileUpload ID="FileUpload_Control" runat="server" />
<asp:Push button ID="FileUpload_SaveBtn" runat="server" Text="Upload" OnClick="FileUpload_SaveBtn_Click" />
<asp:Label ID="FileUpload_Msg" runat="server" Text=""></asp:Label>
</asp:Content>

Footstep six:Copy the below lawmaking in your Default.aspx.cs file.

using Organisation;
using System.Spider web.UI;
namespace SampleWebApplication
{
public partial grade _Default : Page
{
protected void FileUpload_SaveBtn_Click(object sender, EventArgs e)
{
//check if user has selected a file
if (FileUpload_Control.HasFile) {
try
{
/*save file to location.
Make certain the directory path is correct.*/
FileUpload_Control.SaveAs("C:\\FileUploadExample\\" + FileUpload_Control.FileName);
FileUpload_Msg.Text = "File uploaded successfully.";
}
catch
{
FileUpload_Msg.Text = "Mistake - Unable to relieve file. Please try again.";
}
}
else {
FileUpload_Msg.Text = "Error - No file chosen.";
}
}
}
}

Step vii: Run the application. Below is the output of your code. It has a file upload control that allows users to browse and select a file from the local storage.

The code-behind file has a SaveAs() office to the FileUpload object. This method uploads the contents of the file and saves them to the specified directory.

select a file from the local storage.

The higher up is the landing page of your application. Choose a file by clicking on the Cull File push. Once yous have chosen a file, the file name is displayed aslope the FileUpoad control.

asp.net fileupload 5

Click on the Upload button to execute the code behind the method FileUpload_SaveBtn_Click. This method contains the SaveAs phone call to the server.

SaveAs call to the server

Find that the file is indeed saved at the specified path in the server. In our case, our local storage is the server every bit nosotros are running the application on localhost.

Conclusion

To get more advanced training on some of the advanced backdrop and code backside logics, the official Microsoft documentation is highly recommended.

Recommended Manufactures

This is a guide to the ASP.NET FileUpload. Here we discuss the introduction, properties, and various examples of ASP.NET FileUpload. You may also take a look at the following articles to learn more –

  1. RadioButton in ASP.Cyberspace
  2. Ajax in ASP.Internet
  3. Button in ASP.NET?
  4. ASP.NET Image

mansfieldhiceivien77.blogspot.com

Source: https://www.educba.com/asp-dot-net-fileupload/

0 Response to "Getting the Value and Display of Asp File Upload"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel