我正在开发一个程序,如果接收到正确的相对路径,它将返回文件的元数据。
我创建了一个单独的 javascript 模块并导入到 html 文件的脚本标记中。 因此,项目目录看起来像
- index.html
- 元数据.js
Index.html
<script type="module">
import { default as MetaData } from './metadata.js'
const metaClass = new MetaData();
const url = './models/' + 'random.png';
console.log(metaClass.returnmeta(url))
</script>
metadata.js
import { default as ResourceDetect } from '/resourceFile.js'; // additionally used module
export default class MetaData {
getMeta(url) {
const resource = new ResourceDetect();
resource.setfile(url); // renders the file in imported module
resource.Open(() => {
const info = resource.info;
const metaJSON = new Object();
metaJSON.File_Name = info.name
metaJSON.File_Extension = info.extension
metaJSON.File_Size = info.size
metaJSON.File_Frames = info.frames
metaJSON.File_FPS = info.rates
const toJSON = JSON.stringify(metaJSON,null,"\t");
return toJSON
})
}
returnmeta(url) {
return this.getMeta(url)
}
}
如您所见,我通过将 url 作为参数传递来调用模块。但是,作为 resource.Open() 本身,如果是回调函数,return toJSON 不会将任何值传递给 returnmeta 函数,因此我在控制台中收到 undefined窗口。
我想找到一种方法来从回调函数中检索一个值并将相应的json类型对象正确返回到html端。
谢谢!