Search code examples
.netmodulecontent-management-systemdotnetnukedotnetnuke-5

DotNetNuke File Management


I'm working with DNN 5.6.2 and i'm trying to build a custom module where users can upload a file that I run validation against. If the file is valid then it is saved. There is one root level directory "DataValidation" that the module creates and each instance of the module creates a subdirectory to keep things organized.

I have no problems creating the directories but when I upload to them it either fails with various different errors or strangely the file uploads fine but in the DNN database it says that it is in the root level folder, "DataValidation". Is there something wrong with my code or approach?

    // Method is called to create the folders before writing to them
    public void verifyModuleFolderExists(string subfolderName)
    {
        bool moduleFolderExists = false;
        bool instanceFolderExists = false;

        ArrayList folders = FileSystemUtils.GetFolders(PortalId);

        foreach (FolderInfo folder in folders)
        {
            if (folder.FolderPath == "DataValidation/")
            {
                moduleFolderExists = true;
            }

            if (folder.FolderPath == subfolderName)
            {
                instanceFolderExists = true;
            }
        }

        if (!moduleFolderExists)
        {
            FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath, "DataValidation\\");
        }

        if (!instanceFolderExists)
        {
            FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath + "DataValidation\\", subfolderName);
        }
    }

    // Called on file upload
    public void saveUploadedFile(HttpPostedFile uploadedFile, string subFolderName)
    {
        string path = PortalSettings.HomeDirectoryMapPath + "DataValidation\\" + subfolderName + "/";
        string s = FileSystemUtils.UploadFile(path, uploadedFile);
    }

Solution

  • I believe the issue is with the string path = ... part, you have "/" which should really be "\". That would cause it to ignore the folder part of the path potentially.

    So your upload code would change to

    public void saveUploadedFile(HttpPostedFile uploadedFile, string subFolderName)
    {
        string path = PortalSettings.HomeDirectoryMapPath + 
                        "DataValidation\\" + subfolderName + "\\";
        string s = FileSystemUtils.UploadFile(path, uploadedFile);
    }