Download a file keeping original filename when final link is hidden

后端 未结 2 1311
甜味超标
甜味超标 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:11

    I think basename() would be the simplest option https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/basename

    e.g.

    download.file(url, basename(url))

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题