[英]How to save images in a subfolder just created in c#
我正在用代碼創建子文件夾,並希望將圖像保存在該文件夾中
protected void Button1_Click2(object sender, EventArgs e)
{
if (FileUpload1.HasFile && photot == true || FileUpload2.HasFile && photot == true || FileUpload3.HasFile && photot == true || FileUpload4.HasFile && photot == true)
{
string filePath = Path.GetFileName(Server.MapPath("~/Images_Clients/"));
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileNameSecond = Path.GetFileName(FileUpload2.PostedFile.FileName);
string fileNameThird = Path.GetFileName(FileUpload3.PostedFile.FileName);
string fileNameForth = Path.GetFileName(FileUpload4.PostedFile.FileName);
string folderPath = Server.MapPath("~/Images_Clients/");
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileName));
FileUpload2.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileNameSecond));
FileUpload3.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileNameThird));
FileUpload4.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileNameForth));
Response.Redirect(Request.Url.AbsoluteUri);
Response.Redirect(Request.Url.AbsoluteUri);
Response.Redirect(Request.Url.AbsoluteUri);
Response.Redirect(Request.Url.AbsoluteUri);
}
{
}
protected void Button2_Click(object sender, EventArgs e)
{
// Specify a "currently active folder"
myImagePath = Path.GetDirectoryName(Server.MapPath("~/Images_Clients/"));
//Create a new subfolder under the current active folder
newPath = System.IO.Path.Combine(myImagePath,txtFolder.Text );
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
}
如何將圖像保存在僅用代碼創建的新子文件夾中?
看起來您正在使用ASP.NET WebForms上傳create文件夾,然后開始將圖像上傳到該文件夾。
我建議您顯示一棵樹供用戶選擇,然后在每個帖子中發送文件夾路徑。
如果您必須像現在一樣這樣做,那么這可能會有所幫助:
-使用ViewState保存創建的路徑。 -單擊每個按鈕,您應該具有保存的值,並且需要使用該值將圖像上傳到:
protected void Button2_Click(object sender, EventArgs e)
{
/// your code for creating folder goes here...then
ViewState["CreatedPath"] = newPath;
}
在按鈕1中:
protected void Button1_Click2(object sender, EventArgs e)
{
/// your code before FileUpload1.PostedFile.SaveAs line goes here...
/// then use ViewState["CreatedPath"] to get the value you previously saved.
}
新創建的文件夾存儲在newPath
變量中,但是您沒有使用它。 您必須更換
string folderPath = Server.MapPath("~/Images_Clients/");
與
string folderPath = Server.MapPath(newPath);
以及-由於folderPath
當前未被使用-請從folderPath
位置更改所有FileUpload操作
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileName));
至
FileUpload1.PostedFile.SaveAs(Server.MapPath(folderPath+ fileName));
所有這些都假定newPath
是可以在Button1_Click2
訪問的實例或類變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.