Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 7876 |  Welcome, Guest!   Register  Login
Home > Tutorials > ASP.NET Tutorials > FileUpload

Silverlight Tutorials | Report a Bug in the Tutorial
Found interesting? Add this to:


asp:FileUpload control
FileUpload control allows users to upload file to the webserver.
 
Ads
FileUpload control allows users to upload file to the web hosting server. When it is rendered on the page, it is implemented through <input type=file /> HTML tag. Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <input>.
DEMO : FileUpload Show Source Code
              // File upload control
              <asp:FileUpload ID="FileUpload2" runat="Server" />
              
    // Fires when Button is clicked.
    protected void UploadFileOnServer(object sender, EventArgs e)
    {
        // limitation of maximum file size
        int intFileSizeLimit = 10;

        // get the full path of your computer
        string strFileNameWithPath = FileUpload1.PostedFile.FileName;
        // get the extension name of the file
        string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
        // get the filename of user file
        string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
        // get the file size
        int intFileSize = FileUpload1.PostedFile.ContentLength / 1024; // convert into byte

        // Restrict the user to upload only .gif or .jpg file
        strExtensionName = strExtensionName.ToLower();
        if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif"))
        {
            // Rstrict the File Size 
            if (intFileSize < intFileSizeLimit)
            {
                // upload the file on the server
                // you can save the file with any name, However as this is the sample so I have saved the file same name for all users. So it will overwrite your file with next user's who will test this tutorials.
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UserFiles/Samples/") + "SampeFromTutorials" + strExtensionName);

                lblMessage.Text = "Uploaded file details <hr />" +
                    "File path on your Computer: " + strFileNameWithPath + "<br />" +
                    "File Name: " + strFileName + "<br />" +
                    "File Extension Name: " + strExtensionName + "<br />" +
                    "File Size: " + intFileSize.ToString();
            }
            else
            {
                lblMessage.Text = "File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
            }
        }
        else
        {
            lblMessage.Text = "Only .jpg or .gif file are allowed, try again!";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }
    }
                        
                    





About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 8:26:44 PM