簡體   English   中英

動態更改用於上載控件的圖像路徑

[英]change image path for upload control dynamically

我正在使用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/

您說“我正在上載時我需要更改。因為我為不同類型的圖像提供了單獨的文件夾”,因此您可能有一種檢測圖像類型的方法。 假設您已檢測到當前上載圖像的類型並將其存儲在變量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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM