|
|
|
|
www.wesnetdesigns.com Joined: 2/18/2005
Posts: 3253
|
|
|
Since the file has already been saved to the physical file system, you will need to use the lower level methods of the FolderController and FileController classes. Here is a method (in vb, sorry) which I recently wrote for the DNN Gallery module:
' William Severance - added method to add file and optionally a non-existing
' folder to the DNN Files and/or Folders table(s)
' Filepath should be a physical file path, not portal or root relative.
' If AddNonExistingFolder is false, sub exits without adding file if folder is missing
' from DNN folders table, else will first create an entry in the folders table inheriting
' the parent folder's permissions.
' If successful, the FileId will be returned, otherwise a -1.
Public Shared Function SaveDNNFile(ByVal Filepath As String, ByVal Width As Integer, _
ByVal Height As Integer, ByVal AddNonExistingFolder As Boolean, _
ByVal ClearCache As Boolean) As Integer
Dim fileId As Integer = -1
If Not String.IsNullOrEmpty(Filepath) Then
Dim ps As Portals.PortalSettings = Portals.PortalController.GetCurrentPortalSettings()
Dim fi As New System.IO.FileInfo(Filepath)
If fi.Exists Then
Dim folderPath As String = fi.Directory.FullName.Replace(ps.HomeDirectoryMapPath, "").Replace("\", "/")
Dim fileName As String = fi.Name
Dim fileExt As String = fi.Extension.TrimStart("."c).ToLower()
Dim fileSize As Long = fi.Length
Dim PortalId As Integer = ps.PortalId
Dim folderId As Integer = -1
Dim objFolderController As New Services.FileSystem.FolderController
Dim objFolderInfo As Services.FileSystem.FolderInfo = objFolderController.GetFolder(PortalId, folderPath, False)
' Is the folder already in the DNN folders table? If not, should we create it and continue?
If objFolderInfo Is Nothing Then
If AddNonExistingFolder Then
folderId = objFolderController.AddFolder(PortalId, folderPath)
If folderId <> -1 Then
' Set Folder Permissions to inherit from parent
FileSystemUtils.SetFolderPermissions(PortalId, folderId, folderPath)
End If
End If
Else
folderId = objFolderInfo.FolderID
End If
If folderId <> -1 Then
Dim objFileController As New Services.FileSystem.FileController
Dim objFileInfo As Services.FileSystem.FileInfo = objFileController.GetFile(fileName, PortalId, folderId)
Dim contentType As String = FileSystemUtils.GetContentType(fileExt)
If contentType.StartsWith("image") AndAlso (Width = 0 OrElse Height = 0) Then
Dim img As Bitmap = Nothing
Try
img = New Bitmap(Filepath)
Width = img.Width
Height = img.Height
Catch
'eat the exception if we can load bitmap - will have width=height=0
Finally
If img IsNot Nothing Then
img.Dispose()
img = Nothing
End If
End Try
End If
' Is the file already in the DNN files table in which case we update rather than add
If objFileInfo Is Nothing Then
fileId = objFileController.AddFile(PortalId, fileName, fileExt, fileSize, _
Width, Height, contentType, folderPath, folderId, ClearCache)
Else
fileId = objFileInfo.FileId
objFileController.UpdateFile(fileId, fileName, fileExt, fileSize, Width, _
Height, contentType, folderPath, folderId)
End If
End If
End If
End If
Return fileId
End Function
In your application, if you are certain that the destination folder has already been added to the DNN Folders table, you can simplify that portion of the code.
Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
|
|
|
|
| |
|
|
Joined: 12/2/2008
Posts: 118
|
|
|
For posterity:
This approach worked for me, however the GetFolder method of the FolderController class was listed as deprecated in DNN 5, so I guess we can't expect this to work in DNN 6. I'm hoping this is deprecated because DNN will support file system providers, so developers can choose NeatUpload or Flajaxian or Silverlight upload or whatever.
Here's a code snippet in C# for the non-VB people like me. In my application, I knew the file type and folder for all uploads, so I was able to simplify considerably.
// need to do this stuff to get the file into the DNN file system
FolderController flc = new FolderController();
FolderInfo fli = flc.GetFolder(PortalId, "AudioFiles/");
FileController fic = new FileController();
Services.FileSystem.FileInfo fii = fic.GetFile(file.FileName, PortalId, fli.FolderID);
string contenttype = FileSystemUtils.GetContentType("mp3");
if (fii == null) // we need to add the file
{
fileID = fic.AddFile(PortalId, file.FileName, "mp3", mpg.fileSize, 0, 0, contenttype, "", fli.FolderID, true);
}
else // we should update it
{
fileID = fii.FileId;
fic.UpdateFile(fileID, file.FileName, "mp3", mpg.fileSize, 0, 0, contenttype, "", fli.FolderID);
}
talk.AudioURL = "FileID=" + fileID.ToString();
|
|
|
|
| |
|
|
|
www.wesnetdesigns.com Joined: 2/18/2005
Posts: 3253
|
|
|
I'm glad you got it working! In regards to the deprecation of the GetFolder method, note that while this overload:
Public Function GetFolder(ByVal PortalID As Integer, ByVal FolderPath As String) As FolderInfo
has been deprecated in DNN 5.00.01, the overload which I had used:
Public Function GetFolder(ByVal PortalID As Integer, ByVal FolderPath As String, ByVal ignoreCache As Boolean) As FolderInfo
is still available (at this time).
Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
|
|
|
|
| |
|
|
Joined: 12/2/2008
Posts: 118
|
|
|
I've discovered something strange. Files uploaded in this manner are not getting entries in the UrlTracking table. A user clicks on a link to download these files. I create the link with LinkClick like this:
Globals.LinkClick(talk.AudioFile, PortalSettings.ActiveTab.TabID, ModuleId, true, true); //where AudioFile is "FileID=nnn"
Which generates a link like:
http://seattleinsight.org/LinkClick.aspx?fileticket=N2FFyzJHLyQ%3d&tabid=90&mid=825&forcedownload=true
But even through I selected URL Tracking with LinkClick, I see no entry for this file in the UrlTracking table.
I'm wondering if the entry for this file needs to be created in the UrlTracking table upon file upload, and not on the fly by the LinkClick handler. Is that true? Do I need an extra step on upload to get a UrlTracking entry? If so, what's the API for it?
AldenG
|
|
|
|
| |