troubleshooting Question

FileUpload only PDF Files

Avatar of asp_net2
asp_net2Flag for United States of America asked on
.NET ProgrammingASP.NET
23 Comments1 Solution657 ViewsLast Modified:
Hello EE,

I have a scenario that is driving me crazy. I have the following code below that ONLY allows a user to upload .pdf files which is what I need. BUT, if the user does not select a file to upload then the code does not execute and instead the page postback to itself.

I tested my SP and they all seem fine, the problem has to do with my code not allowing updates unless I select a file to upload. If i only update the txtPhysicalDateCompleted Control then the page Postback to itself.

    protected void btn_SaveAnnualPhysical_Click(object sender, EventArgs e)
        // Make sure a file has been successfully uploaded
        if (fuAnnualPhysicalForm.PostedFile == null || string.IsNullOrEmpty(fuAnnualPhysicalForm.PostedFile.FileName) || fuAnnualPhysicalForm.PostedFile.InputStream == null)

        //Make sure we are dealing a .pdf file only
        string extension = Path.GetExtension(fuAnnualPhysicalForm.PostedFile.FileName).ToLower();
        string MIMEType = null;

        switch (extension)
            case ".pdf":
                MIMEType = "application/pdf";
                //Invalid file type Error Message
                lblUploadTypeError.Text = "Only .pdf files are allowed";

        //string MIMEType = fuAnnualPhysicalForm.PostedFile.ContentType;
        string filename = fuAnnualPhysicalForm.PostedFile.FileName.Split(new char[] { '\\' }).Last();
        int fileSize = fuAnnualPhysicalForm.PostedFile.ContentLength;

        // Allow only files less than 1,048,576 bytes (approximately 1 MB) to be uploaded.
        if ((fileSize < 1048576))
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessChoice"].ConnectionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "WellnessChoice_InsertAnnualPhysical";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            // Load PDF InputStream into Byte array
            byte[] imageBytes = new byte[fuAnnualPhysicalForm.PostedFile.InputStream.Length + 1];
            fuAnnualPhysicalForm.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);

            cmd.Parameters.AddWithValue("@pi_id", SqlDbType.Int).Value = hf_AppID.Value;
            cmd.Parameters.AddWithValue("@ap_pdf_file", SqlDbType.Image).Value = imageBytes;
            cmd.Parameters.AddWithValue("@ap_pdf_filename", SqlDbType.VarChar).Value = filename;
            cmd.Parameters.AddWithValue("@ap_pdf_mime", SqlDbType.VarChar).Value = MIMEType;
            cmd.Parameters.AddWithValue("@ap_pdf_size", SqlDbType.VarChar).Value = fileSize;
            cmd.Parameters.AddWithValue("@ap_section_complete", SqlDbType.Int).Value = cb_AnnualPhysical.Checked;

            if (string.IsNullOrEmpty(txtPhysicalDateCompleted.Text))
                cmd.Parameters.AddWithValue("@ap_date", SqlDbType.DateTime).Value = DBNull.Value;
                cmd.Parameters.AddWithValue("@ap_date", SqlDbType.DateTime).Value = txtPhysicalDateCompleted.Text;


            catch (Exception ex)
                lblSaveError.Text = ("Error on insert: " + ex.Message.ToString());


            lblFileSize.Text = "Your File has to be 5 MB or smaller";

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros