Monday, October 15, 2012

How to upload a file with fixed file Size using C# in asp.net

Uploading a PDF or Word File having Limited file Size.

Step 1 : Add File Upload tool from the toolbox.
Fig. 1 : To Add upload tool from toolbox
Step 2:  On Button click event

Calling the function on button click to validate the file uploaded is of the format mentioned and has size below the limit defined. Here the file Size is Maximum of  1 MB.

Following are the code validate it:

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string file = "";
            if (FileUpload1.HasFile)
            {
                file = UploadFile();
            }

        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this, "Error::" + ex);
        }
    }

------------------------------------------------------------------------

private string UploadFile()
    {
        string file = lblGLId.Text + "_" + FileUpload1.FileName;

        if (FileUpload1.PostedFile.ContentLength < 1000000)
        {
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            if ((Convert.ToString(Extension).ToLower() == ".pdf") ||                         
                  (Convert.ToString(Extension).ToLower() == ".doc") ||

                   (Convert.ToString(Extension).ToLower() == ".docx"))
            {
                string path = Server.MapPath(".") + "\\FolderName\\" + file;
                FileUpload1.PostedFile.SaveAs(path);
            }
            else
            {
                DisplayAJAXMessage(this, "Only file with extension .PDF/.DOC/.DOCX Allowed to be Uploaded");
            }
        }
        else
        {
            DisplayAJAXMessage(this, "Maximum file size upto 1MB");
        }
        return file;
    }
---------------------------------------------------------------------------------
    static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }


Note : The "FolderName" should be a folder where the uploaded file is to be kept in project.

View Amit Lal's profile on LinkedIn

No comments:

Post a Comment

Monday, October 15, 2012

How to upload a file with fixed file Size using C# in asp.net

Uploading a PDF or Word File having Limited file Size.

Step 1 : Add File Upload tool from the toolbox.
Fig. 1 : To Add upload tool from toolbox
Step 2:  On Button click event

Calling the function on button click to validate the file uploaded is of the format mentioned and has size below the limit defined. Here the file Size is Maximum of  1 MB.

Following are the code validate it:

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string file = "";
            if (FileUpload1.HasFile)
            {
                file = UploadFile();
            }

        }
        catch (Exception ex)
        {
            DisplayAJAXMessage(this, "Error::" + ex);
        }
    }

------------------------------------------------------------------------

private string UploadFile()
    {
        string file = lblGLId.Text + "_" + FileUpload1.FileName;

        if (FileUpload1.PostedFile.ContentLength < 1000000)
        {
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            if ((Convert.ToString(Extension).ToLower() == ".pdf") ||                         
                  (Convert.ToString(Extension).ToLower() == ".doc") ||

                   (Convert.ToString(Extension).ToLower() == ".docx"))
            {
                string path = Server.MapPath(".") + "\\FolderName\\" + file;
                FileUpload1.PostedFile.SaveAs(path);
            }
            else
            {
                DisplayAJAXMessage(this, "Only file with extension .PDF/.DOC/.DOCX Allowed to be Uploaded");
            }
        }
        else
        {
            DisplayAJAXMessage(this, "Maximum file size upto 1MB");
        }
        return file;
    }
---------------------------------------------------------------------------------
    static public void DisplayAJAXMessage(Control page, string msg)
    {
        string myScript = String.Format("alert('{0}');", msg);
        ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
    }


Note : The "FolderName" should be a folder where the uploaded file is to be kept in project.

View Amit Lal's profile on LinkedIn

No comments:

Post a Comment