I hope you find my research on the new EPiServer CMS Unified File System useful. Here are some example code to get you going. The most tricky part is to manually create a page folder if it does not exsist
Getting a handle to an existing file:
UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;
Get or Create a Page Folder:
int folderId = (int) page.Property["PageFolderID"].Value;
string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
IPageDirectory pageDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath) as IPageDirectory;
string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));
UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;
if (directory == null) {
directory = pageDirectory. CreateSubdirectory(folderId.ToString(), page);
}
NOTE! Folder will not “exist” for real until a file is saved inside it. It is important to call CreateSubdirectory with the page as parameter to setup security correctly!
Getting a handle to a new file:
UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;
if (file == null) {
string virtualDir = VirtualPathUtility.GetDirectory(virtualPath);
UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualDir) as UnifiedDirectory;
return directory.CreateFile(VirtualPathUtility.GetFileName(virtualPath));
}
return file;
Example of reading UnifiedFile:
Bitmap image = null;
using (Stream stream = file.Open()) {
image = (Bitmap) Image.FromStream(stream);
}
Example of writing with UnifiedFile:
using (Stream stream = file.Open(FileMode.Create, FileAccess.Write)) {
image.Save(stream, encoderInfo, encoderParams);
}
-
So one way to upload a file by a visitor from a (public) form would be to use the Unified File System as described above? What I mean is, there is no EPiServer xform way?
-
Thanx for the answer. That is how I will solve the problem.
Well, at least there is no support for file upload in the EPiServer xforms. There seems to be one according to x3c
http://www.w3.org/MarkUp/Forms/2003/xforms-for-html-authors.html#FileSelect
-
Hi Fredrik.
Great posts on the new Unified File System. I have avtually more a question than a comment on your blog.Do you know when in the event stack the filesummary for a UnifiedFile is created and stored ?
I have hooked into the UnifiedDirectory.UnifiedFileAdded event to add som data to the filesummary in code, but at this point the file.Summary is still null, so I cannot add my summary data…
file.Summary.Dictionary["Searchmetadata"] = “some metadata I want to have indexed”;
-
Solved it.
Whenever a file is added the UnifiedFile.UnifiedFileCheckedIn is also called.
I put my code in here and evrything works as a charm
-
Hi Frederik,
Thanks for this post – it cleared up a few things for me – though I can’t seem to get the UnifiedFile.UnifiedFileCheckedIn event to fire when adding a file with:
UnifiedFile uf = pagedir.CreateFile(FileUpLoad.FileName);
Stream fs = uf.Open(FileMode.Create);
System.IO.BinaryWriter sw = new BinaryWriter(fs);
sw.Write(FileUpLoad.FileBytes);
sw.Flush();
sw.Close();
fs.Close();any suggestions?
-
Hi. Fredrik
When I create file in VPP dynamically from stream it stays checked out and I fail to read it. I’m receiving following error: “File has no checked in version and can only be access by the author”. Could you help me with fixing this? -
Thanks, I have figure out this myself.
To have ability to check-in or check-out UnifiedFile file, it should be converted to IVersionFile using method “TryAsVersioningFile”. -
Hi Fredrik
Thanks for this post. It was very useful!
But I got errors (GDI+) when using The suggested Bitmap save method. Instead I used Morten Leerhøy’s stream alternative. It worked fine! Deleted the last line of code: fs.Close();. It raised an exception, because it is already closed by sw.Close();
-
Hi
When trying to get a file by:
UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;I get access denied if i dont set “bypassaccesscheck = true” in web.config. I wonder where i can set credentials for this access if its even possible? Does setting bypassaccesscheck to true make the files unsecure in any way or whats the consequenses?
thanks in advance
-
using EPiServer.Web.Hosting;

![Fredrik Haglund [Photo by: Kristina Sahlén]](http://blog.fredrikhaglund.se/wp-content/uploads/2010/03/fredrik200.jpg)

17 comments
Comments feed for this article
Trackback link: http://blog.fredrikhaglund.se/blog/2007/12/05/episerver-using-the-new-unified-file-system/trackback/