 |
BOOK: Professional DotNetNuke Module Programming ISBN: 978-0-470-17116-5
 | This is the forum to discuss the Wrox book Professional DotNetNuke Module Programming by Mitchel Sellers and Shaun Walker - Wrox DotNetNuke Series Editor; ISBN: 978-0-470-17116-5 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional DotNetNuke Module Programming ISBN: 978-0-470-17116-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

March 11th, 2010, 05:20 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
|
|
uploading a file
The announcement module I'm working on is for a school district. The announcements can have optional file attachments. We can't use the dnn:url control b/c it doesn't allow us to set the default destination folder - it defaults to the portal root. (!!)
I added an asp.net FileUpload control to my form and here is my code so far. It makes sure the destination folder exists and then uploads the file. I verified that the file is physically on the server, and dnn 'knows' that it's there (the file is referenced in the dnn 'files' database table). How can I extract the link to this file so I can store the value in my announcement record? The dnn:url control handled this for you for free. I see that the Utilities.FileSystemUtils.UploadFile function returns a string, but after testing it, the string is empty. And the intellisense doesn't offer any clue as to what the string is supposed to contain.
fileUploadAttachment references the FileUpload control on my web form.
Code:
If fileUploadAttachment.HasFile Then
Utilities.FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath.Replace("/", "\"), "files")
Utilities.FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath.Replace("/", "\") & "files\", "Announcements")
oInfo.AttachmentLink = Utilities.FileSystemUtils.UploadFile(PortalSettings.HomeDirectoryMapPath.Replace("/", "\") & "files\Announcements\", Me.fileUploadAttachment.PostedFile, False)
Else
oInfo.AttachmentLink = ""
End If
|
|

March 12th, 2010, 12:10 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Uploading a file and retrieving the FileID using a FileUpload control - one approach
Spent around 8 hours on this lol, so posting here in hopes that it helps other people. It would be so nice if there was a DNN equivalent of MSDN so developers could see more information on all of the useful and available DotNetNuke classes. It would enable (and encourage) novists like myself to pursue careers in professional DNN sites (meaning, the type that brings money to the DNN organization).
The following function could be be expanded upon to be more universal, but it serves my immediate need. The folder path is hardcoded for this example.
When creating a module and file uploads are required by the users, you need to dictate where the files end up. If you leave it up to the end users to place the files in the correct folder, that's not going to happen. As far as I can tell, the dnn:url control doesn't let you lock down destination folder. A basic ASP.net FileUpload control and a button works well. In my situation, my Announcement object has a url field, linking to the optional file attachment. I could get the file upload working easily enough, but there isn't a way to retrieve the FileId value to store in the Announcement AttachmentURL 'fieID' value. Here's what I came up with, along with some notes.
Code:
Private Function UploadFile(ByVal _FileUpload As FileUpload) As String
' FolderController will be used to retrieve folder information from the folder table
Dim oFoC As New Services.FileSystem.FolderController
' FileController will be used to retrieve file info from the file table
Dim oFiC As New Services.FileSystem.FileController
' DirectoryInfo is an asp.net native class, I'm using this to see if the required upload folder structure already exists
Dim oDirInfo As New IO.DirectoryInfo(PortalSettings.HomeDirectoryMapPath & "files/Announcements")
If Not oDirInfo.Exists Then
' following 2 lines create the physical folder AND add entries into the DNN folders table
' this method needs the complete physical path
' NOTE: AddFolder uses backslashes inside the folderpath but the new foldername does not end with a slash
Utilities.FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath.Replace("/", "\"), "files")
Utilities.FileSystemUtils.AddFolder(PortalSettings, PortalSettings.HomeDirectoryMapPath.Replace("/", "\") & "files\", "Announcements")
' following 2 lines WILL create db entries, but WILL NOT create physical folders
' the first line works
' the second line incorrectly adds the full physical path and creates a mess. the db entry is added, but causes problems that are visible if you view the DNN file manager
' NOTE: - the FolderController class expects forward slashes after the foldernames
' VALID:
'intFolderID = oFC.AddFolder(PortalId, "files/", oFoC.StorageLocationTypes.InsecureFileSystem, False, False)
' INVALID: (completes without errors but creates a mess)
'intFolderID = oFC.AddFolder(PortalId, PortalSettings.HomeDirectoryMapPath & "files/Announcements/")
End If
' now that our folder structure is intact, upload the file to the server
' NOTE: this method can only be used for the InsecureFileFolder type - which is what we want in this case
' NOTE: Utilities.FileSystemUtils.UploadFile uses backslashes
Utilities.FileSystemUtils.UploadFile(PortalSettings.HomeDirectoryMapPath.Replace("/", "\") & "files\Announcements\", _FileUpload.PostedFile, False)
' the folders are created, and the file is uploaded
' now we need to the file ID so we can build the announcement's attachment URL value
' and to do that, we need to back up a step and first get the folderID of the folder we created
' NOTE: Services.FileSystem.FolderInfo uses forwardslashes
Dim foInfo As Services.FileSystem.FolderInfo = oFoC.GetFolder(PortalId, "files/Announcements/")
' armed with the folderID, we can extract the FileID
Dim fiInfo As Services.FileSystem.FileInfo = oFiC.GetFile(_FileUpload.FileName, PortalId, foInfo.FolderID)
' finally (!!) we can get our attachement's URL value (which is simply the FileId - an integer)
' the function has uploaded the file and now it's returning the fileID value to be used by the calling procedure
Return fiInfo.FileId.ToString
End Function
|
|

March 12th, 2010, 12:38 PM
|
|
Wrox Author
|
|
Join Date: Jul 2008
Posts: 74
Thanks: 1
Thanked 8 Times in 8 Posts
|
|
Your implementation works, and we are in progress in trying to get a better process in place to share API documentation.
You could use the URL control and only give editors permissions to a single folder, and then it would only allow them to upload to that folder. It is also possible to set the default folder value, you can look at the dotnetnuke documents module for an example.
Granted, I'll agree that the process to figure out how is not all that easy.
__________________
Mitchel Sellers
Microsoft C# MVP, MCITP
Director of Development
IowaComputerGurus Inc.
My blog for .NET and DotNetNuke info
Author of "Professional DotNetNuke Module Programming"
Tech Editor on "Visual Studio 2010 six-in-one" and "Pro C# 4.0"
|
|

March 12th, 2010, 01:40 PM
|
|
Authorized User
|
|
Join Date: Jan 2010
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Thanks! I think I can utilize your trick in my app.
I don't mind finding help that way. To me that's one of the easiest ways to learn - examining other people's work.
So... What purpose does the "a" serve? What would happen if you used a "b"?
Code:
' Set default folder
ctlUrl.Url = objDocumentsSettings.DefaultFolder & "a"
Coincidentally, my next assignment here at the school district is to develop a "forms and documents" module. I'd like to use the database to store the files, instead of the file system. Just to see if I can do it...
Few questions:
Is the file's content (the bits and bytes that comprise the physical file) stored in the 'Files' table, in the 'Content' field (datatype = image)? I added a file through the DNN file manager as a test and this record is the only one where the 'content' field is not NULL. If it's not saved here, then where does the file go?
What modules or tutorials can you recall that utilize storing files in the DNN database?
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| uploading a file |
MunishBhatia |
ASP.NET 2.0 Professional |
4 |
May 30th, 2007 04:18 AM |
| Uploading File |
misskaos |
Classic ASP Basics |
4 |
October 26th, 2006 02:56 PM |
| file uploading |
lakshmi devi |
Classic ASP Basics |
2 |
September 3rd, 2006 11:52 PM |
| FIle Uploading |
[email protected] |
Classic ASP Basics |
3 |
February 23rd, 2004 12:32 PM |
| File uploading |
zabedin |
Classic ASP Basics |
1 |
July 16th, 2003 08:33 PM |
|
 |