Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想在页面加载时自动将代码文件夹中的文件提取到js文件中_Javascript_Html_Input_Fetch - Fatal编程技术网

Javascript 我想在页面加载时自动将代码文件夹中的文件提取到js文件中

Javascript 我想在页面加载时自动将代码文件夹中的文件提取到js文件中,javascript,html,input,fetch,Javascript,Html,Input,Fetch,我想在页面加载时自动从我的代码文件夹中提取一个文件到我的js文件,这样我就可以在音频可视化工具中使用。我不必从用户电脑上取 我该怎么做? 我试图用fetch来做这件事,但我现在只能从URL获取。 我想像这样使用这个文件 document.getElementById('file').addEventListener('change', function(e){ this.fileReader.readAsArrayBuffer(e.target.files[0]);

我想在页面加载时自动从我的代码文件夹中提取一个文件到我的js文件,这样我就可以在音频可视化工具中使用。我不必从用户电脑上取

我该怎么做? 我试图用fetch来做这件事,但我现在只能从URL获取。 我想像这样使用这个文件

    document.getElementById('file').addEventListener('change', function(e){
      this.fileReader.readAsArrayBuffer(e.target.files[0]);
      }.bind(this));
      var _this = this;
    
    this.fileReader.onload = function(){
      _this.audioContext.decodeAudioData(_this.fileReader.result, function(buffer){
        if(_this.source) {
          _this.source.stop();
        }
        _this.source = _this.audioContext.createBufferSource();
        _this.source.buffer = buffer;
        
        _this.source.loop = true;

        _this.source.connect(_this.analyser);

        _this.gainNode = _this.audioContext.createGain();

        _this.source.connect(_this.gainNode);
        _this.gainNode.connect(_this.audioContext.destination);
        _this.source.start(0);
        
        _this.frequencyArray = _this.webgl.sphereG.attributes.aFrequency.array;
        _this.indexPosArray = _this.webgl.indexPosArray;
        _this.indexPosLength = _this.webgl.indexPosArray.length;
        _this.isReady = true;
      });
    };
  }
但是,我不想让用户从他/她的计算机上选择一个文件,而是想自动从代码所在的文件夹中提取一个文件。

那么就不需要

你也许会成功

// if the HTML that's executing the current JavaScript is at 
// https://example.com/foo/index.html and audiofile.ext
// is in the same location as index.html (i.e. also inside foo),
// this would work:

fetch('audiofile.ext')
   .then(res => res.blob())
   .then(blob => {
      // do stuff with the blob
   });

请参阅。

我尝试了您的建议,但仍显示出一些错误

我在index.js中编写的代码

fetch('roses.mp3')
.then(res => res.blob())
.then(blob => {
  console.log(blob);
});
我在控制台中遇到的错误

第一错误

Fetch API cannot load 
file:///C:/Users/sanid/Desktop/my%20website%20full%20page/roses.mp3. URL scheme must 
be "http" or "https" for CORS request.
第二个错误

index.js:119 Uncaught (in promise) TypeError: Failed to fetch
at index.js:119
我还尝试将源代码更改为file:///C:/Users/sanid/Desktop/my%20website%20full%20page/roses.mp3
但它也不起作用

您显示的代码是用户从计算机中选择文件时运行的代码。如果要加载的文件与脚本文件位于同一文件夹中,则该文件具有URL;与脚本文件相同,仅使用要加载文件的文件名。如果用户未手动选择文件,则无法使用
fileReader
。(未经许可,浏览器无法访问本地文件系统)。这就是为什么您必须使用
fetch()
我不想访问本地文件系统。正如我提到的,音频文件已经在我的代码所在的文件夹中。我刚把它部署到github和tada。。。