本文介绍了隐藏最终链接时,下载一个保留原始文件名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要下载文件,将其保存在文件夹中,同时保留网站中的原始文件名。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我知道我可以使用命令轻松下载 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 befname <- str_match(headers(fil)$`content-disposition`, "\"(.*)\"")[2]# renamefile.rename("tmp.fil", fname) 这篇关于隐藏最终链接时,下载一个保留原始文件名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 21:24