File Upload in ASP.Net:
ASP.Net has two controls that give the users to upload files to the web server. Once the server gets the posted file data, the application may save it, check it or remove it. The subsequent controls allow the file uploading:
- HtmlInputFile - an HTML server control
- FileUpload - and ASP.Net web control
Both the controls gives file uploading, but the FileUpload control automatically gives the encoding of the form, whereas the HtmlInputFile does not do so.
In this part, we will need the FileUpload control. The FileUpload control takes the user to browse for and choose the file to be uploaded, giving a Browse button and a text box for adding the filename.
Once, the user has added the filename in the text box, by selecting the name or browsing, the SaveAs function of the FileUpload control may be known save the file to the disk.
The basic syntax for using the FileUpload is:
<asp:FileUpload ID= "Uploader" runat = "server" />
|
The FileUpload class is takes from the WebControl class, and inherits all its members. Apart from that, the FileUpload class has the subsequent read-only properties:
Properties
|
Description
|
FileBytes
|
Gives an array of the bytes in a file to be uploaded..
|
FileContent
|
Gives the stream object pointing to the file to be uploaded.
|
FileName
|
Gives the name of the file to be uploaded.
|
HasFile
|
Denotes whether the control has a file to upload.
|
PostedFile
|
Gives a reference to the uploaded file.
|
The posted file is encapsulated in an object of type HttpPostedFile, which could be accessed through the PostedFile property of the FileUpload class.
The HttpPostedFile class has the following important properties, which are much used:
Properties
|
Description
|
ContentLength
|
Gives the size of the uploaded file in bytes.
|
ContentType
|
Gives the MIME type of the uploaded file
|
FileName
|
Gives the full filename.
|
InputStream
|
Gives a stream object pointing to the uploaded file.
|
Example:
The following example shows the FileUpload control and its function. The form has a FileUpload control along with a save button and a label control for showing the file name, file length and file type.
In the design view, the form looks like:
![310_file upload.png](https://www.expertsmind.com/CMSImages/310_file%20upload.png)
The content file:
<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"
Width="85px" />
<br /><br />
<asp:Label ID="lblmessage" runat="server" />
</div>
</form>
</body>
|
The code at the back the save button:
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();
}
}
|
Email based ASP.Net assignment help - homework help at Expertsmind
Are you searching ASP.Net expert for help with File Upload in ASP.Net questions? File Upload in ASP.Net topic is not easier to learn without external help? We at www.expertsmind.com offer finest service of ASP.Net assignment help and ASP.Net homework help. Live tutors are available for 24x7 hours helping students in their File Upload in ASP.Net related problems. Computer science programming assignments help making life easy for students. We provide step by step File Upload in ASP.Net question's answers with 100% plagiarism free content. We prepare quality content and notes for File Upload in ASP.Net topic under ASP.Net theory and study material. These are avail for subscribed users and they can get advantages anytime.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving ASP.Net queries in excels and word format.
- Best tutoring assistance 24x7 hours