Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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-从FileReader或base64创建下载链接_Javascript_Html_Base64 - Fatal编程技术网

Javascript-从FileReader或base64创建下载链接

Javascript-从FileReader或base64创建下载链接,javascript,html,base64,Javascript,Html,Base64,我使用下面的代码来加载FileReader来创建一个标记,并将FileReader的结果放在href中。这是一个base64字符串 let reader = new FileReader() reader.readAsDataURL(myInputTypeFile.files[0]) reader.onloadend = (e) => { let file for (let i = 0; i < attachInput.file

我使用下面的代码来加载FileReader来创建一个标记,并将FileReader的结果放在href中。这是一个base64字符串

   let reader = new FileReader()
    reader.readAsDataURL(myInputTypeFile.files[0])

    reader.onloadend = (e) => {
        let file
        for (let i = 0; i < attachInput.files.length; i++) {
            file = attachInput.files[i]

            if (file.type === 'application/pdf'){
                let anchor = document.createElement('a')
                anchor.setAttribute('class', 'q-attached-file')
                anchor.setAttribute('title', file.name)
                anchor.setAttribute('href', reader.result)
                anchor.innerText = file.name
                myElement.appendChild(anchor)
            }
        }
    }
let reader=new FileReader()
reader.readAsDataURL(myInputTypeFile.files[0])
reader.onloadend=(e)=>{
让文件
for(设i=0;i
这是生成的html:

但当我点击元素时,我只看到浏览器上加载了“about:blank”

使现代化 这是reader.result字符串在console.log()中的显示方式

如果用户所在的浏览器支持,则可以将其添加到锚定标记中

<a download href="...">


有一个lib可以做一些棘手的事情来避免浏览器之间的一些差异。如果您不介意增加一些kb,请尝试使用。

Base64字符串作为
href
的字符串完全有效-您确定Base64确实正确吗?您还确定为浏览器的“打开方式”分配了PDF查看器吗?看起来您的数据uri缺少“,”。这是mozilla文档中的数据uri格式:data:[][;base64],可能不是你的问题,但我总是喜欢先从小东西开始。@obsidiange请查看updaterazy,不知怎么的,它现在工作了,以前不工作。谢谢,这将使ios和IE11与其他库一起工作