[英]How to create sub folder using user's userID in main folder?
I am currently one issue - which is to create a sub folder (which is named after each user's ID name).我目前是一个问题 - 这是创建一个子文件夹(以每个用户的 ID 名称命名)。 Following is what I had tried.
以下是我尝试过的。
protected void Button1_Click(object sender, EventArgs e)
{
string filepath = Path.GetExtension(FileUpload1.FileName);
if (filepath.ToLower() != ".pdf" &&
filepath.ToLower() != ".png" &&
filepath.ToLower() != ".gif" &&
filepath.ToLower() != ".zip")
{
lblmessage.Text = "Only pdf, png and gif file are accepted";
}
else
{
if (FileUpload1.PostedFile.ContentLength > 5000000)
{
lblmessage.Text = "Maximum size (5MB) exceeded";
}
}
foreach (HttpPostedFile postedfile in FileUpload1.PostedFiles)
{
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
string filename = Path.GetFileName(postedfile.FileName);
postedfile.SaveAs(Server.MapPath("Courses/FAID_CSP/ADMIN_NO/" + filename));
}
lblmessage.Text = string.Format("{0} Selected Files Are Submitted into DB",
FileUpload1.PostedFiles.Count);
}
} }
Hi try the code below:嗨试试下面的代码:
.......
//Get the admin number from somewhere.
int userId = GetAdminNo();
string subDir = $"Courses/FAID_CSP/{userId}/";
if (!Directory.Exists(subDir))
{
Directory.CreateDirectory(subDir);
}
string filename = Path.GetFileName(postedfile.FileName);
postedfile.SaveAs(Server.MapPath(subDir + filename));
......
I think what you're trying to do is this:我认为你想要做的是:
foreach (HttpPostedFile postedfile in FileUpload1.PostedFiles)
{
string USER_ID = ????
string userPath = Server.MapPath("Courses/FAID_CSP/ADMIN_NO/") + USER_ID;
if (!Directory.Exists(userPath))
{
Directory.CreateDirectory(userPath);
}
string filename = Path.GetFileName(postedfile.FileName);
postedfile.SaveAs(Path.Combine(userPath, filename));
}
You have to provide a way to set USER_ID since you currently don't have that in your code.您必须提供一种设置 USER_ID 的方法,因为您的代码中目前没有该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.