【发布时间】: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; 提取文件名并使用它来构建您的服务器路径