隐藏最终链接时,下载一个保留原始文件名的文件 [英] Download a file keeping original filename when final link is hidden

查看:102
本文介绍了隐藏最终链接时,下载一个保留原始文件名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载文件,将其保存在文件夹中,同时保留网站中的原始文件名。

I need to download a file, save it in a folder while keeping the original filename from the website.

url <- "http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1"

通过网络浏览器,如果单击该链接,则可以下载具有以下文件名的excel文件:

From a web browser, if you click on that link, you get to download an excel file with this filename:

AfiliadosMuni-02-2015.xlsx

AfiliadosMuni-02-2015.xlsx

我知道我可以使用命令轻松下载< R中的em> download.file 像这样:

I know I can easily download it with the command download.file in R like this:

download.file(url, "test.xlsx", method = "curl")

但是我真正需要的脚本是下载并保留原始脚本文件名完整。我也知道我可以从控制台这样使用curl来做到这一点。

But what I really need for my script is to download it keeping the original filename intact. I also know I can do this with curl from my console like this.

curl -O -J $"http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1"

但是,再次,我需要在R脚本中使用它。有没有一种类似于上面的方法,但是在R中?我已经研究过 RCurl 包,但找不到解决方案。

But, again, I need this within an R script. Is there a way similar to the one above but in R? I have looked into the RCurl package but I couldn't find a solution.

推荐答案

您可以总是做类似的事情:

You could always do something like:

library(httr)
library(stringr)

# alternate way to "download.file"
fil <- GET("http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1", 
           write_disk("tmp.fil"))
# get what name the site suggests it shld be
fname <- str_match(headers(fil)$`content-disposition`, "\"(.*)\"")[2]
# rename
file.rename("tmp.fil", fname)

这篇关于隐藏最终链接时,下载一个保留原始文件名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆