简体   繁体   English

如何在主文件夹中使用用户的用户 ID 创建子文件夹?

[英]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.

相关问题 如何在 C# 中创建“递归”文件夹和子文件夹 - How can i create "recursively" folder and sub folder in C# 如何在用户的 AppData 文件夹中的 app 文件夹中创建一个文件夹,例如 C:\\User\\AppData? - How can I create a folder in the app folder in the user's AppData folder e.g. C:\User\AppData? 如何使用 WiX 3.11 安装到子文件夹中 - How to install into a sub folder using WiX 3.11 如何在用户选择的根文件夹中创建一个包含一些子文件夹的文件夹? - How to create a folder having some subfolders, in a root folder selected by the user? 如何在asp.net MVC(C#)中为新注册用户创建用户文件夹? - how to create a user's folder for a new registered user in asp.net MVC (C#)? 如何配置 NLog FileTarget 为每个用户名创建子文件夹? - How to configure NLog FileTarget to create sub-folder for each username? 如何创建以项目ID命名的文件夹 - How to create a folder named by item's ID 在UWP上使用所需的用户名称创建文件夹名称 - Create Folder with the folder name as desired user on UWP 如何将子文件夹从文件夹复制到另一个文件夹? - How to copy sub-folder from folder into another folder? 使用 C# 将图像从阶梯子文件夹移动到主文件夹 - Move Images from ladder Sub folders to Main Folder using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM