如何在输入所需密钥的文件时仅重命名或保留文件名?

发布于 2025-01-27 18:50:30 字数 2167 浏览 3 评论 0原文

请参阅我的下面代码。它工作正常,但是在输入所需键下载文件时,文件名更改,并使用整个域名 +目录 +文件名下载。但是我只想要文件名。

代码:

//这是HTML部分。

<center>
                            <input class="keyBox" style="padding : 10px; padding-left:15px; padding-right:15px" type="text" width="100px" placeholder="Enter your download key">
                            <br><br>
                            <div class="text-center">
                                <button id="down" class="btn btn-style btn-primary">Download</button>
                            </div>
                        </center>

//这是我正在使用的脚本。

<script>
                            const files = [{
                                key: 12345,
                                path: 'Marouf.png'
                            }, {
                                key: 12477,
                                path: 'Ismat.png'
                            }]
                            const globalPath = 'https://abcd.com/directory/certificates/'
                            const inp = document.querySelector('.keyBox')
                            const btn = document.querySelector('#down')
                            btn.addEventListener('click', downloadURI)

                            function downloadURI() {
                                if (inp.value) {
                                    let uri = files.filter(f => f.key === Number(inp.value))
                                    if (uri.length) {
                                        let link = document.createElement("a");
                                        const fullPath = globalPath + uri[0].path
                                        link.download = fullPath;
                                        link.href = fullPath;
                                        link.click();
                                    } else {
                                        alert("Incorrect download key! Try again...")
                                    }
                                }
                            }
                        </script>

See my below code. It's working fine but while downloading a file by entering the required key, the file name changed and it's downloading with the whole domain name + directory + file name. But I want just the file name.

Code :

//This is the HTML part.

<center>
                            <input class="keyBox" style="padding : 10px; padding-left:15px; padding-right:15px" type="text" width="100px" placeholder="Enter your download key">
                            <br><br>
                            <div class="text-center">
                                <button id="down" class="btn btn-style btn-primary">Download</button>
                            </div>
                        </center>

// This is the Script I am using.

<script>
                            const files = [{
                                key: 12345,
                                path: 'Marouf.png'
                            }, {
                                key: 12477,
                                path: 'Ismat.png'
                            }]
                            const globalPath = 'https://abcd.com/directory/certificates/'
                            const inp = document.querySelector('.keyBox')
                            const btn = document.querySelector('#down')
                            btn.addEventListener('click', downloadURI)

                            function downloadURI() {
                                if (inp.value) {
                                    let uri = files.filter(f => f.key === Number(inp.value))
                                    if (uri.length) {
                                        let link = document.createElement("a");
                                        const fullPath = globalPath + uri[0].path
                                        link.download = fullPath;
                                        link.href = fullPath;
                                        link.click();
                                    } else {
                                        alert("Incorrect download key! Try again...")
                                    }
                                }
                            }
                        </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-02-03 18:50:30

有多种方法可以解决您的问题。最简单的解决方案是设置下载属性而无需值,

link.download = '';

这将使用URL的最终段作为文件名。

下载

使浏览器将链接的URL视为下载。可以在有或没有值的情况下使用:

没有一个值,浏览器将建议从各种来源生成的文件名/扩展名:

  • 内容分配HTTP标头
  • URL路径中的最后一部分
  • 媒体类型(来自内容类型的标题,数据的开始:url或blob.type for Blob:url)

定义值表明它是文件名。 /和\字符转换为下划线(_)。文件系统可能禁止
文件名中的其他字符,因此浏览器将调整建议
如有必要。

mdn

There are multiple ways to solve your problem. The most simple solution is to set the download attribute without value

link.download = '';

This will use the final segment of the URL as filename.

download

Causes the browser to treat the linked URL as a download. Can be used with or without a value:

Without a value, the browser will suggest a filename/extension, generated from various sources:

  • The Content-Disposition HTTP header
  • The final segment in the URL path
  • The media type (from the Content-Type header, the start of a data: URL, or Blob.type for a blob: URL)

Defining a value suggests it as the filename. / and \ characters are converted to underscores (_). Filesystems may forbid
other characters in filenames, so browsers will adjust the suggested
name if necessary.

MDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文