【问题标题】:ASP FileUpload suddenly not workingASP FileUpload 突然无法正常工作
【发布时间】:2013-06-05 07:11:40
【问题描述】:

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

下面是我上传功能的代码。

        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 函数会运行,但是只会创建目录,但不会将文件上传到目录中。

【问题讨论】:

  • 请在您生成的 HTML 页面中显示使用呈现的 <form> 元素。我怀疑您缺少正确的 enctype 属性。
  • @Dai
  • 这对我来说看起来不错,您是否尝试过调试和单步调试代码来识别问题?
  • @Dai 是的,我在上面解释了我为确定问题所做的工作。问题似乎出在if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".png")
  • FileUpload1.HasFile.FileName 的值是多少?它可能会失败,因为您正在对文件扩展名进行区分大小写的比较(考虑调用.ToUpperInvariant().Equals(".png", StringComparison.OrdinalIgnoreCase。另请注意,检查文件扩展名并不能提供安全性:您应该尝试验证上传的内容实际上是接受之前的 PNG 图像。

标签: c# asp.net sql file-upload


【解决方案1】:

您的表单中有更新面板吗?可以在 Page、MasterPage 或 UserControl 中。

FileUpload(或input type='file')不适用于异步(Ajax)回发。要解决此问题,请删除 UpdatePanel,或将提交按钮设置为 PostBackTrigger 或在此处查看我的答案:https://stackoverflow.com/a/3868293/245581

【讨论】:

  • 是的,问题似乎与更新面板有关。 FileUpload 对象在更新面板中将始终为空。
猜你喜欢
  • 1970-01-01
  • 2014-12-10
  • 2013-11-16
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 2018-04-30
相关资源
最近更新 更多