ASP FileUpload突然不工作

本文关键字:工作 突然 FileUpload ASP | 更新日期: 2023-09-27 18:16:53

所以我的fileupload函数已经工作了一段时间,但突然它停止了工作,即使在我在一个新项目中测试它并恢复到旧版本的代码后,问题仍然存在。

下面是我的上传函数的代码。

        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.");
    }
    }

所以基本上我被带到else函数,因为if函数似乎不正确。然而,我可以发誓,它工作了大约2周,然后突然不起作用。我还将if函数设置为

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

测试我的if else要求语句是否正确,当我这样做时,if函数运行,但是只有目录被创建,但文件没有上传到目录。

ASP FileUpload突然不工作

是否有一个UpdatePanel在你的表单?可以在Page, MasterPage或UserControl中。

FileUpload(或input type='file')不能很好地与异步(Ajax)回发一起工作。要解决此问题,请删除UpdatePanel,或将提交按钮设置为PostBackTrigger或查看我的答案:https://stackoverflow.com/a/3868293/245581