I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks something like this:
public FileContentResult GetDocument(String pathName)
{
try
{
Byte[] buffer = BlobStorage.DownloadFile(pathName);
FileContentResult result = new FileContentResult(buffer, "PDF");
String[] folders = pathName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
// get the last one as actual "file name" based on some convention
result.FileDownloadName = folders[folders.Length - 1];
return result;
}
catch (Exception ex)
{
// log error
}
// how to handle if file is not found?
return new FileContentResult(new byte[] { }, "PDF");
}
The BlobStorage
class there is my helper class to download the stream from the blob.
My question is stated in the code comment: How should I handle the scenario when the file/stream is not found? Currently, I am passing an empty PDF file, which I feel is not the best way to do it.
window.location.href = "GetDocument?pathName=" + docPath;
. That will call the controller action above and respond accordingly. – Rafferty