首页 > 解决方案 > 通过 R 中的管道工 API 上传多个文件

问题描述

我正在REST API使用R 中的 Plumber 进行开发。在其中我有一个模块,我支持上传多个 CEL 文件并运行算法,获取输出并将其显示在前端。

我参考了这段代码,但我很困惑这段代码是否正确。

管道工_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
    }

start_plumber_api.R

plumber::plumb("C:/wamp64/www/testing/upload.R")$run(port = 1234)

HTML

<form action="http://127.0.0.1:1234/uploadfile" method="POST" enctype="multipart/form-data">
    <label>Upload CEL File</label><br>
    <input type="file" id="files" accept=".CEL, .gz" multiple />
    <input type="submit" value="upload">
  </form>

收到此错误

Warning in Rook::Multipart$parse(req) : bad content body

谁能告诉我这个代码上传CEL文件是否正确,还有谁能解释一下这个错误是什么意思??????

请帮忙!

先感谢您

标签: rapifile-uploaduploadplumber

解决方案


如果有人像我一样面临同样的问题。

你可以参考这个 Github 链接:-

https://github.com/rstudio/plumber/issues/579

#* @param f:[file]
#* @post /upload
function(f) {
  names(f)
}

此功能将使您能够一次上传多个文件,无论它可能是任何文件,并且在上传文件之前,您必须限制要上传的文件作为输入。


推荐阅读