DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Switching to FileManager

 9 Replies
 3 Subscribed to this topic
 26 Subscribed to this forum
Sort:
Author
Messages
New Around Here
Posts: 6
New Around Here

hi -- I've inherited a DNN website that mostly consists of custom modules.

In trying to upgrade the site, I found a class that relies heavily on FileController to add files to the system and I've had to rewrite some of the code. Most of it I've figured out but I'm completely stumped on this one method and can't find an example anywhere.

The old code was this:

System.IO.FileInfo fi = new System.IO.FileInfo(mySecuredFilename);
                String contentType = FileManager.Instance.GetContentType(fileExtension);
                DotNetNuke.Services.FileSystem.FileInfo myFileInfo = new DotNetNuke.Services.FileSystem.FileInfo();
                myFileInfo.FileName = fi.Name.Replace(Globals.glbProtectedExtension, "");
                myFileInfo.Extension = fileExtension;
                myFileInfo.ContentType = contentType;
                myFileInfo.Size = Convert.ToInt32(fi.Length);
                myFileInfo.FolderId = myFolderId;
                myFileInfo.PortalId = PortalId;
                myFileInfo.Width = 0;
                myFileInfo.Height = 0;
                myFileInfo.Folder = myFolderInfo.FolderName;

                try
                {
                    myFileId = myFileController.AddFile(myFileInfo);
                } catch (Exception exc)
                {

 

I'm stumped on how to use the FileManager.AddFile method properly. I found the following but it throws some kind of date error:

System.IO.FileStream myStream = new System.IO.FileStream(myFileName, System.IO.FileMode.Create);

            try
            {
                
                myFileManager.AddFile(myFolderInfo, myFileName, myStream);
            }
            catch (Exception ex)
             {

            }

 

Could someone please give me a working example of how to add a file with FileManager?

 

Veteran Member
Posts: 1246
Veteran Member
MVP
MVP
You're an MVP!
I'm assuming that the module compiles. Is that right?

What is the error?
New Around Here
Posts: 6
New Around Here
hi Joe,

The error is:
"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."

I'm pretty stumped on why it would throw a date error.
New Around Here
Posts: 6
New Around Here

sorry, forgot to say that the module does compile and everything works fine until it gets to myFileManager.AddFile

Veteran Member
Posts: 1246
Veteran Member
MVP
MVP
You're an MVP!
Is there a date field that you didn't initialize?
New Around Here
Posts: 6
New Around Here
not that I can see. When I examine FolderInfo properties, all the date fields contain dates (UpdatedOn, CreatedOn etc)
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!

Can you post the full stack trace from the exception? That would help narrow down which part of the code is throwing the exception. For example, if it's the call to <code><a href="https://github.com/dnnsoftware/Dnn.Platform/blob/db80b65cfc2fb3920db380c46bdaf6525c1d1f62/DNN%20Platform/Library/Services/FileSystem/FileManager.cs#L307"> DataProvider.Instance().UpdateFileLastModificationTime</a></code> then the issue has to do with how the folder provider gets the last modified time (for example, if a custom folder provider returns <code>DateTime.MinDate</code> instead of <code><a href="https://dnndocs.com/api/DotNetNuke.Common.Utilities.Null.html#DotNetNuke_Common_Utilities_Null_NullDate">Null.NullDate</a></code>).

New Around Here
Posts: 6
New Around Here
I'm not even clear what FileManager is doing -- adding a record of the file to a db table? The file (a spreadsheet) has already been saved into a folder at this point.

sorry to be murky but I didn't create this module and not quite sure why the AddFile is being called
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!

<code>AddFile</code> may be used to create a file record in the database, but it can also be used to update the contents of a file. Since you're passing a <code>Stream</code>, I think it's probably attempting to update the contents of the file to match the contents in the <code>Stream</code>. I would guess that you want a read-only <code>Stream</code> rather than a <code>System.IO.FileMode.Create</code> <code>Stream</code> so that the code can read the contents, but I don't think that would be related to the date error you're getting now.

New Around Here
Posts: 6
New Around Here
hi Brian, thanks for that info.

The stack trace is:

at DotNetNuke.Services.FileSystem.FileManager.AddFile(IFolderInfo folder, String fileName, Stream fileContent, Boolean overwrite, Boolean checkPermissions, Boolean ignoreWhiteList, String contentType, Int32 createdByUserID)
at DotNetNuke.Services.FileSystem.FileManager.AddFile(IFolderInfo folder, String fileName, Stream fileContent)
at FileUtils.AddFileToSystem(String myFileName, Int32 PortalId, String folderPath, String fileExtension) in [my path to App_Code] \FileUtils\FileUtils.cs:line 162

These Forums are for the discussion of the open source CMS DNN platform and ecosystem.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
  2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
  3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  5. No Flaming or Trolling.
  6. No Profanity, Racism, or Prejudice.
  7. Site Moderators have the final word on approving / removing a thread or post or comment.
  8. English language posting only, please.

Would you like to help us?

Awesome! Simply post in the forums using the link below and we'll get you started.

Get Involved