Home > DeveloperSection > Blogs > File Uploading in ASP.NET

File Uploading in ASP.NET


.NET C#  ASP.Net 
Ratings:
0 Comment(s)
 596  View(s)
Rate this:

File Uploading in ASP.NET

This blog talks about File Uploading  in ASP.NET.

The  fileUpload  control allows the users to browse  for and select the file to be uploaded,  providing a browse button and a text box for entering the filename.

Syntax of fileupload:

<asp:FileUpload ID=”Uploader” runat=”server”>

Properties                                                     Description

FileBytes                                                  Returns an array of the  bytes in a file to be uploaded.

FileContent                                              Returns the stream object pointing to the file to be   Uploaded.

FileName                                                Returns the  name of the file to be upload.

HasFile                                                Specifies whether the control has a  file to upload.

PostFile                                                Returns a reference to the uploaded file.

 Example

protected void bntUpload_Click(object sender, EventArgs e)

    {

        string res = string.Empty;

        if (browserFile.HasFile)

        {

           try

            {

                String FileExtension = System.IO.Path.GetExtension(browserFile.FileName);

                if (FileExtension == ".xml")

                {

                    if (browserFile.PostedFile.ContentLength < 262144)

                    {

                        string fileName = browserFile.FileName.ToString();

                        string uploadFolderPath = "~/XMLFiles/";

                        string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);

                        browserFile.SaveAs(filePath + "\\"  + fileName);

                        objClassFile.UploadXmlFile(out res, fileName);

                        lblMsg.Text = res;

                        listBoxEmpId.Items.Clear();

                        bindDataList();

                     

                    }

                    else

                        lblMsg.Text = "Upload status: The file has to be less than 256 kb!";

                }

                else

                    lblMsg.Text = "Upload status: Only XML files are accepted!";

            }

            catch (Exception ex)

            {

                lblMsg.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;

            }

        }

        else

            lblMsg.Text = "Please select Data XML file";

    }}


Don't want to miss updates? Please click the below button!

Follow MindStick