我正在创建一个包含代码的子文件夹,并希望将图像保存在该文件夹中
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);
}
如何在刚刚使用代码创建的新子文件夹中保存图像?
答案 0 :(得分:0)
看起来您正在使用ASP.NET WebForms上传创建文件夹,然后开始将图像上传到它
我建议您显示一个树供用户选择,然后发送每个帖子的文件夹路径
如果你必须像现在这样做,那么这可能会有所帮助:
- 使用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.
}
答案 1 :(得分:0)
新创建的文件夹存储在newPath
变量中,但您没有使用它。
您必须更换
string folderPath = Server.MapPath("~/Images_Clients/");
与
string folderPath = Server.MapPath(newPath);
和 - 由于当前未使用folderPath
- 从
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images_Clients/" + fileName));
到
FileUpload1.PostedFile.SaveAs(Server.MapPath(folderPath+ fileName));
所有这些假设newPath
是可在Button1_Click2