动态更改上传控件的图像路径

时间:2013-01-25 07:36:08

标签: c# asp.net

我在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/

1 个答案:

答案 0 :(得分: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);