Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
    }
}
Posted
Comments
Richard Deeming 20-Sep-19 10:56am    
REPOST
This is now your THIRD copy of exactly the same question:
Asp.net file upload control problem[^]
Repost. Asp.net 4.6.1. Upload control problem[^]

STOP.

If you're not getting the answer you want, don't just repost the same question again and again. All you're doing is annoying the people who answer questions here.

Instead, think about how you could improve your question. What additional information you might need to add.

Then go back to your original question, click the green "Improve question" link, and improve your question.
Member 10548977 20-Sep-19 11:18am    
Right. I'm not confused about the issue I'm having. I put what I've tried text in the wrong text boxes in this portal.

Do you understand the issue I'm having?
Richard Deeming 20-Sep-19 11:19am    
The issue you're having is that you keep reposting the same question, rather than editing the original question.
[no name] 20-Sep-19 11:31am    
It's just "bad" code. Bad enough, most people will pass after a minute.
ZurdoDev 20-Sep-19 13:56pm    
So? Who cares if the file is still in the control. Why is that a problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900