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?
sorry, forgot to say that the module does compile and everything works fine until it gets to myFileManager.AddFile
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>).
<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.
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:
Awesome! Simply post in the forums using the link below and we'll get you started.