问题

一些可在浏览器中打开的文件,如pdf,浏览器会直接打开它,而不是下载。

a标签的download属性

在a标签上添加download属性,如:

<a href="test.pdf" download></a>

测试后发现Chrome和Firefox会直接下载,而在IE和Edge中仍然会打开pdf。

Content-Disposition响应头

在文件请求的响应头中添加:Content-Disposition:attachment,发现在Chrome\Firefox\IE\Edge中均为直接下载文件

以下为node express测试代码

//下载
//如,访问http://localhost:3942/download/test.pdf会下载test.pdf
app.use('/download', express.static('downloadFiles', {
    setHeaders: function (res, path, stat) {
        res.set({
            'Content-Disposition': 'attachment',
        })
    }
}))
//打开
//如,访问http://localhost:3942/browse/test.pdf会在浏览器中打开test.pdf
app.use('/browse', express.static('downloadFiles', {
    setHeaders: function (res, path, stat) {
        res.set({
            'Content-Disposition': 'inline',
        })
    }
}))
分类: javascript

12 条评论

g thing · 2020年6月20日 下午1:14

whoah this blog is magnificent i like reading your articles.
Stay up the great work! You realize, lots of persons are hunting
round for this information, you can aid them greatly.

g see · 2020年6月21日 上午10:18

After going over a few of the articles on your site, I seriously
like your way of blogging. I book-marked it to my bookmark site list and will be
checking back in the near future. Please visit my website as well
and let me know your opinion.

https://cotkan.ru/user/KourtneyWhite76 · 2020年6月26日 下午12:15

An impressive share! I’ve just forwarded this onto a colleague who was doing
a little homework on this. And he actually ordered me dinner due to the fact that I stumbled
upon it for him… lol. So allow me to reword this…. Thanks for the meal!!
But yeah, thanks for spending time to discuss this matter here on your site.

tinyurl.com · 2020年6月26日 下午5:47

Hi there, I would like to subscribe for this weblog to take most up-to-date
updates, therefore where can i do it please assist.

verksamhet.se · 2020年6月27日 上午1:58

With havin so much content do you ever run into any issues
of plagorism or copyright infringement? My site has a lot of completely unique content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over
the web without my authorization. Do you know any techniques to help protect against content from being
ripped off? I’d genuinely appreciate it.

tinyurl.com · 2020年6月27日 上午9:43

It’s really a nice and useful piece of information. I am satisfied that you shared this helpful
information with us. Please stay us up to date like this.
Thank you for sharing.

cbd oil that works 2020 · 2020年6月27日 下午4:36

Today, I went to the beach front with my children. I found a
sea shell and gave it to my 4 year old daughter and said
“You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.

There was a hermit crab inside and it pinched her ear. She never
wants to go back! LoL I know this is totally off topic but I had to tell someone!

cbd oil that works 2020 · 2020年6月27日 下午7:37

It’s amazing in favor of me to have a website, which is good in favor of my knowledge.
thanks admin

cbd oil that works 2020 · 2020年6月28日 上午3:47

I think everything published made a great deal of
sense. However, consider this, suppose you were to
create a awesome headline? I mean, I don’t want to tell you how to run your
website, however suppose you added a title to maybe get a person’s attention?
I mean 如何使浏览器强制下载文件 –
霜月秋风 is kinda vanilla. You ought to look at Yahoo’s front page and note how
they create article titles to get viewers interested.

You might add a related video or a pic or two to grab people excited about what
you’ve got to say. In my opinion, it might bring your website a little bit
more interesting.

cbd oil that works 2020 · 2020年6月28日 上午6:24

I’m really impressed with your writing skills as
well as with the layout on your blog. Is this a paid theme or
did you customize it yourself? Either way keep up the excellent quality writing, it’s rare to see a nice blog like this
one today.

cbd oil that works 2020 · 2020年6月29日 上午6:41

Have you ever considered about including a little bit more than just
your articles? I mean, what you say is important and everything.
But just imagine if you added some great images or video clips to give your posts more,
“pop”! Your content is excellent but with pics and video clips, this site could certainly be
one of the very best in its field. Amazing blog!

cbd oil that works 2020 · 2020年6月29日 下午12:13

Wow, wonderful blog layout! How long have you been blogging for?

you make blogging look easy. The overall look of your web site is excellent, let
alone the content!

发表评论

电子邮件地址不会被公开。 必填项已用*标注