0

I have a MVC project that allows users to upload files to their account and make them accesible through a link to the filepath on a datatable, when the user uploads their content the file gets renamed based on the original name and a timestamp:

if (uploadedF.ContentLength > 0)
{
    InputFileName = Path.GetFileName(uploadedF.FileName);
    ServerSavePath = string.Concat(Server.MapPath("~/filesUP/") + YMD + "_" + fHM + "_" + InputFileName);
    //Save file to server folder  
    uploadedF.SaveAs(ServerSavePath);
}

Then the ServerSavePath variable gets stored in the SQL Database as is, so the url is like so:

C:\inetpub\wwwroot\projectname\filesUP\20200309_1047_filename.jpg

Calling the link to the datatable:

{
    "data": "fileUrl",
    "render": function (data, type, row, meta) {
        if (type === 'display') {
            data = '<a href="' + data + '">' + 'Check Document' + '</a>';
        }
        return data;
    }
}

And the browser shows the path like this:

file:///C:/inetpub/wwwroot/projectname/filesUP/20200309_1047_filename.jpg

When clicking the link the browser does nothing but opening it on a new tab the url looks like this on Chrome about:blank#blocked.

IIS has all rights to access the project folders, tried debugging and the problem persists either way. Is there other way to access a file directly from the browser?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Ali
  • 85
  • 6
  • 1
    The problem is because you're using a local filesystem path to the file. You instead need to use the path on the webserver, ie. from the root of your website – Rory McCrossan Mar 09 '20 at 14:01
  • Any source where I could do that? I get that I'm calling the complete file tree of the server but do I have to create a different space to store files? @RoryMcCrossan – Ali Mar 09 '20 at 14:06
  • The browser does not allow web servers to read the local file system. use the relative path instead of the local file path. you could refer this links:[link1](https://stackoverflow.com/questions/39007243/cannot-open-local-file-chrome-not-allowed-to-load-local-resource), [link2](https://stackoverflow.com/questions/38159480/not-allowed-to-load-local-resource) – Jalpa Panchal Mar 10 '20 at 03:27

0 Answers0