Click here to Skip to main content
14,384,533 members
Rate this:
Please Sign up or sign in to 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.
Gerry Schmitz 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?
Member 10548977 20-Sep-19 14:04pm
   
That's not the problem. When someone selects a file to upload then decides not to upload the file by removing the file from the control's textbox. The control's text box value retains the name and path of the file. Therefore the path and name of the file is uploaded even though the user removed the file from the control.
ZurdoDev 20-Sep-19 14:06pm
   
What file upload control are you using?
Member 10548977 20-Sep-19 14:15pm
   
The plain vanilla upload control from asp.net 4.6.1.
ZurdoDev 20-Sep-19 14:18pm
   
Do this check:
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
Member 10548977 20-Sep-19 14:31pm
   
Thx but the check is ignored. Goes thru the condition as if the value was never removed.
ZurdoDev 20-Sep-19 14:34pm
   
You are saying that PostedFile IS null and/or PostedFile.ContentLength IS > 0?

Is this something you can reproduce?
Member 10548977 20-Sep-19 14:41pm
   
PostedFile is Not null and ContentLegth > 0.

Although I manually removed the file from the control's text value.
ZurdoDev 20-Sep-19 14:49pm
   
What browser?
Member 10548977 20-Sep-19 14:58pm
   
IE 11.737. Chrome's behavior is completely different. Chrome doesn't use a text box to hold the file instead the user is given a remove file clickable link to remove the file.
ZurdoDev 20-Sep-19 14:51pm
   
Member 10548977 20-Sep-19 15:02pm
   
Thank you. I'll take a look.
Member 10548977 20-Sep-19 14:17pm
   
FileUpload

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




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