May 13, 2021 ASP.NET
This section describes two ASP.NET to upload files to a Web page that are used in the web page.
ASP.NET contains two controls that users can upload files to the web server. O nce the server accepts the uploaded file data, the application can save, examine, or ignore it. T he next control allows the file to be uploaded:
Both controls allow file uploads, but the FileUpload control automatically formats the encoding, which is not the case with htmlInputFile controls.
In this guide, we'll apply the FileUpload control. T his control allows the user to preview the selection of the file to be uploaded, it provides a preview button and a text box where the file name can be entered.
Once the user enters the file name or previews the file in the text box, the SaveAs method of the FileUpload control saves the file to the hard disk.
FileUpload's basic syntax is as follows:
<asp:FileUpload ID= "Uploader" runat = "server" />
The FileUpload class is derived from the WebControl class, and it inherits all of its elements, and the FileUpload class has the following read-only properties:
Property | Describe |
---|---|
FileBytes | Returns a set of bytecodes for which the file will be uploaded |
FileContent | Returns the stream object of the file to be uploaded |
FileName | Returns the name of the file that will be uploaded |
HasFile | Determines if the control has files that need to be uploaded |
PostedFile | Return a reference to the uploaded file |
The published file is encapsulated as an object in the form of HttpPostedFile, which can be accessed through the PostedFile property of the FileUpload class.
The HttpPostedFile class has the following commonly used properties:
Property | Describe |
---|---|
ContentLength | Returns the byte size of the uploaded file |
ContentT ype | Returns the MIME type of the uploaded file |
FileName | Returns the full name of the file |
InputStream | Returns the stream object of the file to be uploaded |
The following example illustrates the FileUpload control and its properties. T his table has a FileUpload control as well as a save button and a label control with a real file name, type, and length.
In design mode, the table looks like this:
The relevant file codes are listed below:
<body>
<form id="form1" runat="server">
<div>
<h3> File Upload:</h3>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Button ID="btnsave" runat="server" onclick="btnsave_Click" Text="Save" style="width:85px" />
<br /><br />
<asp:Label ID="lblmessage" runat="server" />
</div>
</form>
</body>
The code for the save button is listed below:
protected void btnsave_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
if (FileUpload1.HasFile)
{
try
{
sb.AppendFormat(" Uploading file: {0}", FileUpload1.FileName);
//saving the file
FileUpload1.SaveAs("<c:\\SaveDirectory>" + FileUpload1.FileName);
//Showing the file information
sb.AppendFormat("<br/> Save As: {0}", FileUpload1.PostedFile.FileName);
sb.AppendFormat("<br/> File type: {0}", FileUpload1.PostedFile.ContentType);
sb.AppendFormat("<br/> File length: {0}", FileUpload1.PostedFile.ContentLength);
sb.AppendFormat("<br/> File name: {0}", FileUpload1.PostedFile.FileName);
}catch (Exception ex)
{
sb.Append("<br/> Error <br/>");
sb.AppendFormat("Unable to save file <br/> {0}", ex.Message);
}
}
else
{
lblmessage.Text = sb.ToString();
}
}
Note the following questions: