我之前已经构建了 API(在 R 中使用 Plumber)并且可以处理字符串输入——我只是很难将文件作为输入参数。管道工文档都没有解释如何做到这一点。这甚至可以做到吗?有没有办法在 Python 中做到这一点?
大话西游666
浏览 170回答 1
1回答
开心每一天1111
您可以使用plumber和Rook使用 POST 上传文件。这是一个小例子api.Rlibrary(plumber)library(Rook)#* Upload file#* @param upload File to upload#* @post /uploadfilefunction(req, res){ fileInfo <- list(formContents = Rook::Multipart$parse(req)) ## The file is downloaded in a temporary folder tmpfile <- fileInfo$formContents$upload$tempfile ## Copy the file to a new folder, with its original name fn <- file.path('~/Downloads', fileInfo$formContents$upload$filename) file.copy(tmpfile, fn) ## Send a message with the location of the file res$body <- paste0("Your file is now stored in ", fn, "\n") res}运行服务器plumber::plumb('api.R')$run(port = 1234)使用 curl 发送文件 test.txtcurl -v -F upload=@test.txt http://localhost:1234/uploadfile