如何防止PDF文件自动下载

i want to display pdf files in my website but unfortunately it will automatically download by idm. here's my code: $

    <div class="viewerthesispdf">
    <div id="viewer" class="pdf-viewer" data-url="<?php the_field('upload_pdfACField'); ?>"></div>

douhu7807
douhu7807 您可能需要提供有关环境的更多信息
接近 3 年之前 回复
douzhan1963
douzhan1963 这可能是服务器配置问题,也可能是您用来读取该数据网址的某个库
接近 3 年之前 回复

2个回答

data-url is used to embed a file in a web page. That makes sense for images. It doesn't make sense for a PDF (or Excel or Word or most other file types) because normally these types of files are in place of a page, not a section of a web page. There are generally two solutions, depending on whether the files need to be restricted:

  1. Use an href tag to reference the file. <a href="https://www.example.com/file1.pdf" target="_blank">Download File1</a> I usually include target="_blank" in order to force a new page so that if there is a problem with the download you don't "lose" the original page, but it isn't strictly necessary. This will simply give a link to the file - and you can use a button, Font Awesome, images, etc. to make it fancier - but in the end "a link to a file".
  2. Create a form which returns only the PDF as a result. Again I usually include target="_blank". The advantage of a form is that (a) the file doesn't have to actually exist on disk - it can be created by a script on-the-fly (technically this can be done with a link too - but typically a link would be a file rather than a script) and (b) you can include any necessary parameters for security, user-specific customization, etc. as part of the form. The form can use a GET or a POST depending on your preference.

Use href to show your pdf file list like this <a href="<?php the_field('upload_pdfACField'); ?>"> Filename </a>, it will not start downloading automatically, only when user click on it, it will start download.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问