1

So my fileupload function had been working for awhile but all of a sudden it had stopped working and the problem remained even after I tested it in a new project and reverted to the older version of the code.

Below is the code of my upload function.

        public void EnsureDirectoriesExist()
{
    string currentuser = "admin";
    //string currentuser = (string)(Session["uname"]);
    // if the \pix directory doesn't exist - create it. 
    if (!System.IO.Directory.Exists(Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/")))
    {
        System.IO.Directory.CreateDirectory(Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/"));
    }

}

protected void uploadbtn_Click(object sender, EventArgs e)
{
    string currentuser = "admin";
    //string currentuser = (string)(Session["uname"]);
    if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".png")
    {
        // create posted file
        // make sure we have a place for the file in the directory structure
        EnsureDirectoriesExist();
        String filePath = Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/" + FileUpload1.FileName);
        String filePath2 = ("~/userimages/" + currentuser + "/displaypicture/" + FileUpload1.FileName);
        FileUpload1.SaveAs(filePath);
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ToString());

        string mySQL, mySQL2;


        mySQL2 = "DELETE FROM displaypicture WHERE username='" + currentuser + "'";
        mySQL = "INSERT INTO displaypicture(username,path) VALUES ('" + currentuser + "','" + filePath2 + "')";
        conn.Open();
        SqlCommand cmdAdd2 = new SqlCommand(mySQL2, conn);
        SqlCommand cmdAdd = new SqlCommand(mySQL, conn);
        cmdAdd2.ExecuteNonQuery();
        cmdAdd.ExecuteNonQuery();

        conn.Close();


        MessageBox.Show("Upload successful!");

    }
    else
    {
        MessageBox.Show("Upload has failed. Please check file format.");
    }
    }

So basically I get brought to the else function as the if function doesn't seem to be correct. However I could have sworn that it worked for about 2 weeks before suddenly not working. I have also switch the if function to,

if(currentuser.equals("admin")){
}

to test if my if else requirement statement was correct and when I did that, the if function gets run however only the directories are created but the file isn't uploaded into the directory.

Damienn
  • 65
  • 1
  • 3
  • 8
  • Please show use the rendered `
    ` element in your generated HTML page. I suspect you're missing the correct `enctype` attribute.
    – Dai Jun 05 '13 at 07:48
  • that looks fine to me, have you tried debugging and stepping-through your code to identify the problem? – Dai Jun 05 '13 at 07:51
  • @Dai Yeah, I explained above what I did to identify the issue. Problem seems to lie at `if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".png")` – Damienn Jun 05 '13 at 07:52
  • What is the value of `FileUpload1.HasFile` and `.FileName`? It might be failing because you're performing a case-sensitive comparison of file extensions (consider calling `.ToUpperInvariant()` or `.Equals(".png", StringComparison.OrdinalIgnoreCase`. Also note that checking the filename extension does not provide security: you should try to verify what was uploaded is actually a PNG image before accepting it. – Dai Jun 05 '13 at 07:59
  • @Dai FileUpload1 is the ID of my FileUpload object and the FileName is the name of the extenension of the file I am uploading, "filename".png. I have tried .Equals and IgnoreCase but it still skips my if statement and jumps right to the else statement. – Damienn Jun 05 '13 at 08:03

1 Answers1

0

Is there an UpdatePanel in your form? Could be in the Page, the MasterPage or in a UserControl.

FileUpload (or input type='file') does not work nicely with an asynchronous (Ajax) postback. To fix this problem remove the UpdatePanel, or set the submit button as PostBackTrigger or see my answer here: https://stackoverflow.com/a/3868293/245581

Community
  • 1
  • 1
Willem
  • 5,364
  • 2
  • 23
  • 44
  • Yes, the problem seem to have something to do with the updatepanel. The FileUpload object will always be empty in the updatepanel. – Damienn Jun 05 '13 at 08:20