0

我在 ASP.NET 和 C# 中工作。

我的应用程序中有一个Fileupload控件。我想在上传过程中更改图像的存储路径(在硬盘上)。我该怎么做。

C#代码:

     string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
    //Save images into Images folder
    fileuploadimages.SaveAs(Server.MapPath("**~/database/**" + filename));
    //Getting dbconnection from web.config connectionstring
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    //Open the database connection
    con.Open();
    //Query to insert images path and name into database
    SqlCommand cmd = new SqlCommand("Insert into tblImageupload1(Imagename,Imagepath) values(@ImageName,@ImagePath)", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@Imagename", filename);
    cmd.Parameters.AddWithValue("@Imagepath", "../Database/" + filename);
    //cmd.Parameters.Add("@price");
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();

注意:我想~/database/在上传期间动态更改路径。

4

1 回答 1

1

你说“当我上传时我需要改变。因为我有不同类型的图像的单独文件夹”,所以你可能有一种方法来检测图像的类型。假设您已经检测到当前上传图像的类型并将其存储在变量 中imagetype

string imagetype = ""; // type of image is here, i.e. "Bird", "Forest", "Mountain"

string imagepath = "/images";
switch (imagetype)
{
  case "Bird": imagepath = "/ImagesOfBirds"; break;
  case "Forest": imagepath = "/ImagesOfForests"; break;
  case "Mountain": imagepath = "/ImagesOfMountains"; break;
}

string path = Path.Combine(imagepath, filename);
fileuploadimages.SaveAs(path);
于 2013-01-25T07:51:07.587 回答