Download a file keeping original filename when final link is hidden

后端 未结 2 1349
甜味超标
甜味超标 2021-01-18 12:59

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?IdcSe         


        
2条回答
  •  温柔的废话
    2021-01-18 13:24

    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)
    

提交回复
热议问题