JS获取IIS虚拟目录文件列表

一、IIS 服务器配置
启用目录浏览
打开 IIS 管理器 → 选择目标网站或虚拟目录 → 双击 "目录浏览" → 右侧点击 "启用"
确保勾选 "时间"、"大小"、"扩展名" 等显示选项(可选)。
配置 MIME 类型(可选)
若需直接返回文件列表的 JSON 数据(而非 HTML),需添加 MIME 类型:
右键网站 → "MIME 类型" → 添加:
扩展名:.json
MIME 类型:application/json
跨域访问配置(CORS)
若请求跨域,需在 IIS 中添加响应头:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Methods" value="GET" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

二、JS访问代码

esriRequest(attachbaseUrl + results.features[0].attributes.xh, {
              responseType: "text" // 接收原始 HTML 内容
            }).then(function (response) {
            const parser = new DOMParser();
            const htmlDoc = parser.parseFromString(response.data, "text/html");
            const links = htmlDoc.querySelectorAll("a"); // 提取所有链接
            const fileList = Array.from(links)
              .map(link => link.getAttribute("href"))
              .filter(name => !name.endsWith("/")); // 过滤子目录

              fileList.forEach(function (attachment) {
                if (attachment.split(".")[1] === "jpg") {
                  const image = document.createElement("img");
                  image.src = attachment;

                  image.className = "queryImg";
                  document.getElementById("queryResults").appendChild(image);
                } else if (attachment.split(".")[1] === "mp4") {
                  const videodiv = document.createElement("video");
                  videodiv.src = attachment;
                  videodiv.autoplay = true;
                  videodiv.muted = true; // 关键:绕过浏览器自动播放限制[1,3](@ref)
                  videodiv.loop = true; 
                  videodiv.className = "queryImg";
                  document.getElementById("queryResults").appendChild(videodiv);
                }
                else{
                  
                }
              });

三、效果


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容