我有一个excel文件,其中包含公司名称和其.pdf文件的可下载链接。我的目的是根据excel列中的公司名称创建目录,并将pdf文件下载到新创建的目录中。excel表包含以下链接:- www.xyz.com/filehandler/filedisplay.aspx?fn=blahblahblahh&fname=ContractingCompanyname 因此,必须在下载文件功能中使用“ https://”后附加它。同样,以上链接也指向我公司的安全站点,该站点在手动运行时会下载pdf文件。
我的问题是PDF文件已下载。但是,打开PDF file opening error image文件时会引发错误。
我已经引用了许多站点,并使用了所有内容,包括RCurl,curl以及R console和R studio来运行我的代码。每次下载文件时,打开时都会引发相同的错误。
这是我的代码
##Set the working directory
txtsrc<-"C:\\Users\\skrishnan\\Desktop\\HR needed\\text analysis proj\\pdf\\FirstAid"
setwd(txtsrc)
##make a vector of contractor's safety program's raw txt file names
pdflist <- read.xlsx("Final results_6thjuly.xlsx",1)
##Check records
head(pdflist)
tail(pdflist)
colnames(pdflist)
##Check if docs folder exists
if (dir.exists("FirstAid_docs")=="FALSE"){
dir.create("FirstAid_docs")
}
##Change the working directory
newfolder<-c("FirstAid_docs")
newpath<-file.path(txtsrc,newfolder)
setwd(newpath)
##Check the present working directory
getwd()
## Create directories and download files
for( i in 1:length(pdflist[,c("ci_CompanyName")])){
##First delete the existing directories
if(dir.exists(pdflist[,c("ci_CompanyName")][i])=="TRUE"){
unlink(pdflist[,c("ci_CompanyName")][i], recursive = TRUE)
}
##Create a new directory
directoryname<-pdflist[,c("ci_CompanyName")][i]
dir.create(directoryname,recursive = FALSE, mode = "644")
##Get the downloadable links
link<-pdflist[,c("DocLink")][i]
vallink<-c("https://")
newlink<-paste0(vallink,link)
stringlink<-as.character(newlink)
pdffilename<-paste0(strsplit(stringlink,"&fname=")[[1]][2],".pdf")
##Set paths for the downloadble file
newway<-file.path(newpath,directoryname)
destfile<-paste(newway,pdffilename,sep="/")
##Download the file
download.file(stringlink,destfile,mode="w",method="auto",quiet = FALSE, cacheOK = TRUE)
##Next record
i<-i+1
}
一些有助于理解代码的东西
> R.Version()
$`platform`
[1] "x86_64-w64-mingw32"
$arch
[1] "x86_64"
$os
[1] "mingw32"
$system
[1] "x86_64, mingw32"
$status
[1] ""
$major
[1] "3"
$minor
[1] "5.0"
$year
[1] "2018"
$month
[1] "04"
$day
[1] "23"
$`svn rev`
[1] "74626"
$language
[1] "R"
$version.string
[1] "R version 3.5.0 (2018-04-23)"
$nickname
[1] "Joy in Playing"
> colnames(pdflist)
[1] "ci_CompanyID" "ci_CompanyName" "ProgramScore" "ID_DI" "DocLink"
非常感谢您的帮助。