Hi,
The code looks right. You would probably check if the file is null or not, before doing anything with it. The error you're getting is that the file is null, i.e. the file was not found.
A more safe way to get a file:
public static class UnifiedFileFunctions
{
private static readonly ILog Log = LogManager.GetLogger(typeof(UnifiedFileFunctions));
public static bool TryGetFile(string path, out UnifiedFile file)
{
file = null;
path = HttpUtility.UrlDecode(path);
if (path == null)
{
return false;
}
try
{
if (!HostingEnvironment.VirtualPathProvider.FileExists(path))
{
Log.InfoFormat("File '{0}' does not exist.", path);
return false;
}
file = HostingEnvironment.VirtualPathProvider.GetFile(path) as UnifiedFile;
if (file == null)
{
Log.InfoFormat("Null when trying to get file '{0}'.", path);
return false;
}
if (file.QueryDistinctAccess(AccessLevel.Read) == false)
{
Log.InfoFormat("User did not have read access to file '{0}'.", path);
return false;
}
return true;
}
catch (Exception ex)
{
Log.Error("File could not be loaded.", ex);
return false;
}
}
}
Hi Johan,
Thank you for your response, I've tried it and I'm still not getting anything back. I don't understand why it's null - I know the file is there. Perhaps its the path I'm passing through - '\globalassets\documents\gt8amniocentesis0111.pdf'.
I've uploaded the file to the directory and in my page I have a Url Property that allows me to browse to that file - it is the value of that property that I'm using as the file path - would this be an issue?
Thanks
Paul
Maybe I'm confusing myself here, when we upload a file it's as a BLOB and it ends up in the folder appdata/blobs- is this the same thing as a VPP and if not how can you determine file size from a document stored in the database.
Hi,
I've uploaded a document to the VPP, and I've since linked to it in a page. Im trying to display the file extension and file size next to the link. I've been searching thorugh the forums and the closest I've come is the following:-
filepath = "\globalassets\documents\gt8amniocentesis0111.pdf"
var file = HostingEnvironment.VirtualPathProvider.GetFile(filepath) as UnifiedFile;
return file.Length / 1024 + "Kb"
Except each time I try to use this I get the error:-
Object Reference is not set to an instance of a object