Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Creating file download linkCreating file download link
Previous
 
Next
New Post
4/16/2009 9:23 PM
 

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
 
New Post
4/19/2009 9:26 PM
 

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();

 
New Post
4/19/2009 11:35 PM
 

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
 
New Post
8/14/2009 1:57 PM
 

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

 
Previous
 
Next
HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...Creating file download linkCreating file download link


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

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

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. 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.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out