File not found in WebGL

I’m new to unity webgl,the below code works well in windows desktop unity (in output i get “File Found”), but in javascript(webgl) it doesn’t work.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class checkPath : MonoBehaviour {

    // Use this for initialization
    void Start ()
    {
        string filePath = "D:/testFile.txt";
        Debug.Log("Looking for file: " + filePath);
        if (File.Exists(filePath))
        {
            Debug.Log("File Found");
        }
        else Debug.Log("File not Found");
    }
 
    // Update is called once per frame
    void Update () {
     
    }
}

steps to reproduce the message:

  • create a empty unity project.
  • attach above script to any gameobject.
  • build the project to webgl
  • open the generated html file

note: i tried running the html file from vscode server still it shows “File not Found” in browser console(both Firefox and Chrome)

below is the output from browser console

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/WebGL/runtime/DebugBindings.gen.cpp Line: 51)

File not Found
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/WebGL/runtime/DebugBindings.gen.cpp Line: 51)```

i am using unity 2017.4.12f1 i had tried with unity 2019 still getting same message

**Update**: i tried copying testFile.txt to build folder and in TemplateData folder as well.
in code i added
```if (File.Exists("file:///testFile"))
if (File.Exists("/testFile"))
if (File.Exists("testFile"))```

none of these returns true in js (browser console)!

as far as I know you can not access the local file system with webgl.

1 Like

In WebGL/WASM builds File.* methods are mapped to IndexedDB, which allows File.WriteAllText and FileWriteAllBytes to work.

did you ever get this to work?

You can create and delete files. The files live in IndexedDB, a browser-based database. Pretty sure that existing files cannot be accessed on the file system.

Hello!

Browsers can’t access local files for security reasons, so the file you’re trying to open will need to be hosted using either a local or remote webserver.

I hope that helps!