ReadAllBytesAsync(path) path always combined with the contentrootpath

632 Views Asked by At

I want to return a files from virtual path directory, currently this is my code in C# asp net core.

     var path = GetConfig.AppSetting["VirtualDirectoryPath:PathLocal"] + "/" + 
                _.data.Downloadpath;
     var provider = new FileExtensionContentTypeProvider();
     if (!provider.TryGetContentType(path, out var contentType))
     {
         contentType = "application/octet-stream";
     }

     var bytes = await System.IO.File.ReadAllBytesAsync("http://website.com/foldername/foldername/filename.pdf");

     return File(bytes, contentType, Path.GetFileName(path));

the DownloadPath contains 'foldername/filename.pdf', and the virtual directory path contains the localhost domain, i tried to hardcode the path but it keeps returning "D://folder/folder/http://website.com/foldername/foldername/filename.pdf". I don't know why the url combined with the contentrootpath, can someone help me?

Actually, i also want to retrieve a virtual directory path as url from the virtual directory that i already created in the iis so that i can download the files from the url. I don't know how to code better, if you can help me with this too i'd very much appreciate it. TIA.

1

There are 1 best solutions below

0
Blindy On

Ultimately, the problem is that you seem to believe File.ReadAllBytesAsync downloads files from the web, it does not. It only reads files on your local file system.

And because http:// files aren't supported, they're not considered to be rooted, and so it gets concatenated to the "current folder" and you get what you see.

Also never concatenate paths like that, that's what Path.Combine is for.