Help shape the future of CMS PaaS release notes! Take this quick survey and share your feedback.
AI OnAI Off
Help shape the future of CMS PaaS release notes! Take this quick survey and share your feedback.
Hi Per.
I usally use this approach:
public static void SaveFile(string newpath, byte[] bytes)
{
UnifiedDirectory dir;
string virtualDir = VirtualPathUtility.GetDirectory(newpath);
string virtualFile = VirtualPathUtility.GetFileName(newpath);
if (HostingEnvironment.VirtualPathProvider.DirectoryExists(virtualDir))
dir = VirtualPathHandler.Instance.GetDirectory(virtualDir, true) as UnifiedDirectory;
else
dir = UnifiedDirectory.CreateDirectory(virtualDir);
if (dir != null)
{
dir.BypassAccessCheck = true;
UnifiedFile newFile = dir.CreateFile(virtualFile);
Stream newImage = newFile.Open(FileMode.Create, FileAccess.Write);
newImage.Write(bytes, 0, bytes.Length);
newImage.Close();
}
}
newpath is the path to the new file, while bytes is the an bytearray of the file to save.
Hope this helps!
BR, Tore
Hi
A have a function that shall create a new UnifiedDirectory and then save file in it but a only get
System.NullReferenceException: Objektreferensen har inte angetts till en instans av ett objekt
when directory bellow is equal with null or do not exist.
Have found some links like
http://blog.fredrikhaglund.se/blog/2007/12/05/episerver-using-the-new-unified-file-system/
That did help a lot but this last part will not work hmmmm...
UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(path) as UnifiedDirectory;
if (directory != null)
file2 = directory.CreateFile(VirtualPathUtility.GetFileName(this.m_targetPath));
else //this do not work
UnifiedDirectory ud = UnifiedDirectory.CreateDirectory(path);
file2 = ud.CreateFile(VirtualPathUtility.GetFileName(this.m_targetPath));