为了账号安全,请及时绑定邮箱和手机立即绑定

将文件作为输入参数的 API

将文件作为输入参数的 API

大话西游666 2022-06-02 18:04:24
我被要求创建一个 API,它将文件作为输入并将该文件放在服务器上的目录中。这是为了消除应用程序直接与文件夹交互的需要。我之前已经构建了 API(在 R 中使用 Plumber)并且可以处理字符串输入——我只是很难将文件作为输入参数。管道工文档都没有解释如何做到这一点。这甚至可以做到吗?有没有办法在 Python 中做到这一点?
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

您可以使用plumber和Rook使用 POST 上传文件。


这是一个小例子


api.R


library(plumber)

library(Rook)


#* Upload file

#* @param upload File to upload

#* @post /uploadfile

function(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.txt


curl -v -F upload=@test.txt http://localhost:1234/uploadfile


查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号