asp.net-mvc - asp.net MVC 使用 DropZone 上传带有子文件夹路径的文件

标签 asp.net-mvc file upload dropzone.js

阿萨拉穆莱库姆, 在 ASP.NET MVC 中,我可以通过 DropZone 上传文件。在这里,如果我拖放任何包含子文件夹和文件的文件夹,它只会上传服务器上传文件夹中所有子文件夹中的所有文件。但我想上传这些文件及其子文件夹名称。该子文件夹名称可以存储在数据库中或创建新文件夹。

例如上传 Google 云端硬盘中的文件/文件夹。

这是我的上传代码(这里只有功能代码):

public void Upload()
{
    bool isSavedSuccessfully = true;
    string fName = "";

    try
    {
        foreach (string fileName in Request.Files)
        {

            HttpPostedFileBase file = Request.Files[fileName];
            fName = file.FileName;


            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    Guid GuidFileName = Guid.NewGuid();
                    var path = Path.Combine(Server.MapPath("~/_UploadedFile"));
                    string pathString = System.IO.Path.Combine(path.ToString());
                    var fileName1 = Path.GetFileName(file.FileName);
                    var ext = Path.GetExtension(file.FileName);

                    bool isExists = System.IO.Directory.Exists(pathString);
                    if (!isExists) System.IO.Directory.CreateDirectory(pathString);
                    var uploadpath = string.Format("{0}\\{1}{2}", pathString, GuidFileName.ToString(), ext.ToString());
                    file.SaveAs(uploadpath);
                }
                catch (Exception ex)
                {

                }
            }
        }

    }
    catch (Exception ex)
    {
        isSavedSuccessfully = false;
    }
    if (isSavedSuccessfully)
    {

    }
    else
    {

    }

}

现在,请帮我从文件中获取子文件夹的名称。

谢谢。

最佳答案

使用 ASP.NET MVC 中的 Dropzone 库上传文件及其子文件夹名称。您需要修改处理文件上传的服务器端代码,以在文件路径中包含子文件夹名称。

示例

public void Upload()
{
    bool isSavedSuccessfully = true;
    string fName = "";

    try
    {
        foreach (string fileName in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[fileName];
            fName = file.FileName;

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    Guid GuidFileName = Guid.NewGuid();

                    // Get the subfolder name from a separate input field or database
                    string subfolderName = "my_subfolder";

                    var path = Path.Combine(Server.MapPath("~/_UploadedFile"), subfolderName);
                    string pathString = System.IO.Path.Combine(path.ToString());
                    var fileName1 = Path.GetFileName(file.FileName);
                    var ext = Path.GetExtension(file.FileName);

                    bool isExists = System.IO.Directory.Exists(pathString);
                    if (!isExists) System.IO.Directory.CreateDirectory(pathString);
                    var uploadpath = string.Format("{0}\\{1}{2}", pathString, GuidFileName.ToString(), ext.ToString());
                    file.SaveAs(uploadpath);
                }
                catch (Exception ex)
                {
                    // Handle any exceptions that occur during file upload
                }
            }
        }
    }
    catch (Exception ex)
    {
        isSavedSuccessfully = false;
        // Handle any exceptions that occur during the file upload process
    }

    if (isSavedSuccessfully)
    {
        // Handle the success case
    }
    else
    {
        // Handle the failure case
    }
}

在此修改后的代码中,子文件夹名称是从单独的输入字段或数据库获取的(在本例中,子文件夹名称被硬编码为“my_subfolder”以供说明),并使用 Path.Combine 方法附加到上传路径。这将在 _UploadedFile 目录中创建一个具有指定子文件夹名称的新子文件夹,并使用 Guid 生成的唯一文件名将上传的文件保存在该子文件夹中。

关于asp.net-mvc - asp.net MVC 使用 DropZone 上传带有子文件夹路径的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75506288/

相关文章:

具有接口(interface)的 C# MVC 通用存储库类

c - 打开文件会导致 sYSMALLOc 断言失败

c - 高效地读取 C 中的扁平化文件

mysql - 如何上传文件(csv 或文本)并在 Zope 框架中读取内容

asp.net-mvc - 自定义身份验证 asp.net MVC

jquery - 数据表和jsonresult的排序问题

c# - 动态 HTML 内容 - "A potentially dangerous Request.Form value was detected from the client"

regex - 检索文件名的一部分

php - 试图在我的 "Edit form"中显示上传的图片

R Shiny : Uploading a file on button click