Click here to Skip to main content
14,424,238 members

Asp.net file upload control problem

Member 10548977 asked:

Open original thread
3rd Time a charm.  
ASP.Net 4.6.1. I have an upload control on my form. When a user selects a file for upload then decides not to upload and remove the file the control retains the file even after being deleted from the control's text box. I do have filters on file type and file size.  


What I have tried:

protected void btnUploadPartCDoc_Click(object sender, EventArgs e)
{

    /* ************************************************************************************************ */
    /*  12/15/2016 RR - Upload file size and type are set in the web.config file.                       */
    /*                - Limit File Size to 5MB per upload/client.                                       */
    /*                - File Type comma delimeted string.                                               */
    /* ************************************************************************************************ */
    string providerCode = Session["providerCode"].ToString();
    string loadFile = string.Empty;
    string maxFileSize = ConfigurationManager.AppSettings["MaxUploadFileSize"];
    IEnumerable<string> allowedFileTypes = ConfigurationManager.AppSettings["AllowedUploadFileTypes"].Split(',');
    string saveDir = @"Document\Refugee\";
    string appPath = Request.PhysicalApplicationPath.ToString(); ;
    string Lst3 = string.Empty;
    string oARegNum = string.Empty;
    string imagePath = string.Empty;
    string savePath = string.Empty;
    string folderPath = string.Empty;
    int fileSize = 0;
    bool isValidFileType = false;

    // --------------------------------------------------------
    // -- To Do:
    // --------------------------------------------------------
    // -- some providers on upload are upload this control type
    // -- rather than the physical image.
    // --------------------------------------------------------
    // -- System.Web.UI.WebControls.FileUpload
    // --------------------------------------------------------
    string fileExt = System.IO.Path.GetExtension(fleUpload.FileName).ToString().ToLower();
    if (fleUpload.HasFile)
    {
        if (!fleUpload.FileName.ToString().Contains("System.Web.UI.WebControls.FileUpload"))
        {
            if (Session["workingProvider"] != null) { string provID = Session["workingProvider"].ToString(); }
            oARegNum = Session["oARegNum"].ToString();
            imagePath = fleUpload.FileName.ToString();
            imagePath = imagePath.Replace(",", "");
            savePath = appPath + saveDir + imagePath;
            folderPath = appPath + saveDir;
            fileSize = fleUpload.FileBytes.Length;

            foreach (string uploadfileType in allowedFileTypes)
            {
                if (fileExt == uploadfileType)
                {
                    isValidFileType = true;
                    break;
                }
            }
            if (!isValidFileType)
            {
                hdnFieldAlert.Value = "error; ERROR! File not allowed.;Document Upload";
                return;
            }
            else
            {
                if (fileSize <= Int32.Parse(maxFileSize))
                {
                    if (Utilities.IsNumeric(oARegNum))
                        try
                        {
                            hdnFieldAlert.Value = "info; Changes have been saved;Record Update";
                            if (hdnFieldAlert.Value != "info; Changes have been saved;Record Update")
                            {
                                hdnFieldAlert.Value = "error; ERROR! You must first Save Refugee Detail form.;Document Upload";
                                return;
                            }

                            if (File.Exists(savePath)) File.Delete(savePath);
                            fleUpload.SaveAs(savePath);

                            SqlConnection con = new SqlConnection(getConnectionString("dbConn").ToString());
                            SqlCommand sqlCmd = new SqlCommand("update [dbo].[provref] set oPartCDocUrl = @imgPath where oprovcode = @providerCode and [oAregNum] = @clientNum", con);
                            sqlCmd.Parameters.Add("@imgPath", SqlDbType.VarChar).Value = imagePath;
                            sqlCmd.Parameters.Add("@clientNum", SqlDbType.VarChar).Value = oARegNum;
                            sqlCmd.Parameters.Add("@providerCode", SqlDbType.VarChar).Value = providerCode;
                            con.Open();
                            sqlCmd.ExecuteNonQuery();
                            con.Close();

                            // ****************************************************************************************
                            // reload form with newly uploaded image
                            // ****************************************************************************************
                            provRefSelectByRecordId();

                            hdnFieldAlert.Value = "info; Changes have been saved. File successfully uploaded.;Document Upload";
                        }
                        catch (Exception ex)
                        {
                            hdnFieldAlert.Value = "error; ERROR!  " + ex.Message.ToString() + " ;Document Upload";
                        }
                }
                else
                {
                    hdnFieldAlert.Value = "error; ERROR! File size too large.;Document Upload";
                    return;
                }
                return;
            }
        }
        else
        {
            hdnFieldAlert.Value = "error; ERROR! Bad File. File Not Uploaded.;Document Upload";
            return;
        }
    }
    else
    {
        hdnFieldAlert.Value = "error; ERROR! Bad File. File Not Uploaded.;Document Upload";
        return;
    }
}
Tags: C#4.0

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100