【问题标题】:Could not find a part of the path 'D:\home\site\wwwroot\DriverImage\image.jfif'找不到路径“D:\home\site\wwwroot\DriverImage\image.jfif”的一部分
【发布时间】:2021-12-22 02:14:11
【问题描述】:

当我尝试提交此表单时,我收到此错误。当我在本地运行此应用程序时,它可以按预期完美运行,但现在我已经在 azure 上部署了该应用程序,我收到此错误:“找不到路径的一部分 'D:\home\site\wwwroot\DriverImage\ image.jfif'。”

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str);
            if (imageUpload.HasFile)
            {
                string isassigned = "no";
                string filename = imageUpload.PostedFile.FileName;
                string filepath = "DriverImage/" + imageUpload.FileName;
                imageUpload.PostedFile.SaveAs(Server.MapPath("~/DriverImage/") + filename);
                con.Open();
                SqlCommand cmd = new SqlCommand("Insert into Drivers (firstname, lastname, gender, race, dob, image, isassigned) values('" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + drpGender.SelectedItem.Text + "', '" + drpRace.SelectedItem.Text + "', '" + Calendar1.SelectedDate.ToString("dd/MM/yyyy") + "', '" + filepath + "', '" + isassigned + "')", con);
                cmd.ExecuteNonQuery();
                con.Close();
                Response.Write("<script>alert('Driver added successfully.');</script>");
                Response.Redirect("AddDriver.aspx");
            }
        }

我有另一个用于不同表单的文件上传控件,它基本上使用相同的代码,但将图像保存在不同的文件夹中,并且在部署时可以工作。

【问题讨论】:

  • 此代码是创建Sql Injection hack的绝佳机会
  • 与您的问题完全无关,但非常中肯的建议:永远不要使用字符串连接 ("Hello " + "World") 来构造 SQL 查询,这会使您容易受到 SQL Injection 的攻击。在 C# 中,我们使用 here 解释的参数化查询来安全地将用户输入添加到查询中
  • 您是否尝试调试此代码? imageUpload.PostedFile.FileName 应该是 获取客户端上文件的完全限定名称。 根据文档。因此,您可以在服务器端代码上使用任何东西。只需使用 Path.GetFilename 或 imageUpload.FileName; 提取文件名并使用它来构建您的服务器路径

标签: c# sql asp.net webforms


【解决方案1】:

在string isassigned = "no";之前添加以下代码

string folderPath = Server.MapPath("~/DriverImage/");

这将在服务器上创建一个文件夹路径

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2013-01-13
    • 2017-05-03
    相关资源
    最近更新 更多