如何在 API 中使用 Plumber 将多张图片上传到子目录?
How to use Plumber in an API to upload multiple images to a sub directory?
我有一个位于 'data/images/' 的子目录,我需要 API 服务将图像上传到该子目录。我在这里使用 R 和 Plumber。我了解基本设置,但我似乎无法获取代码以将上传的文件传送到我的目录中。
这是我的尝试:
图书馆(水管工)
图书馆(车)
#* Upload file
#* @param req:[file]
#* @post /uploadfile
function(req, res){
names(req)
print(names(req))
fileInfo <- list(formContents = Rook::Multipart$parse(req))
print(fileInfo)
## 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(paste0("data/images/",req, sepp=''))
file.copy(tmpfile, fn)
print(fn)
## Send a message with the location of the file
res$body <- paste0("Your file is now stored in ", fn, "\n")
res
}
如有任何帮助,我们将不胜感激。
经过一番扯扯,这基本上就是允许您将图片上传到指定子目录的代码。此代码需要 plumber 和 Rook 包才能工作:
library(plumber)
library(Rook)
#* @param req:[file]
#* @post /upload_test27
function(req, res) {
# Required for multiple file uploads
names(req)
# Parses into a Rook multipart file type;needed for API conversions
fileInfo <- list(formContents = Rook::Multipart$parse(req))
# This is where the file name is stored
# print(fileInfo$formContents$req$filename)
file_name <- fileInfo$formContents$req$filename
# The file is downloaded in a temporary folder
tmpfile <- fileInfo$formContents$req$tempfile
# Create a file path
fn <- (paste0("data/images/",file_name, sepp=''))
#Copies the file into the designated folder
file.copy(tmpfile, fn)
res$body <- paste0("Your file is now stored in ", fn, "\n")
res
}
我有一个位于 'data/images/' 的子目录,我需要 API 服务将图像上传到该子目录。我在这里使用 R 和 Plumber。我了解基本设置,但我似乎无法获取代码以将上传的文件传送到我的目录中。
这是我的尝试:
图书馆(水管工) 图书馆(车)
#* Upload file
#* @param req:[file]
#* @post /uploadfile
function(req, res){
names(req)
print(names(req))
fileInfo <- list(formContents = Rook::Multipart$parse(req))
print(fileInfo)
## 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(paste0("data/images/",req, sepp=''))
file.copy(tmpfile, fn)
print(fn)
## Send a message with the location of the file
res$body <- paste0("Your file is now stored in ", fn, "\n")
res
}
如有任何帮助,我们将不胜感激。
经过一番扯扯,这基本上就是允许您将图片上传到指定子目录的代码。此代码需要 plumber 和 Rook 包才能工作:
library(plumber)
library(Rook)
#* @param req:[file]
#* @post /upload_test27
function(req, res) {
# Required for multiple file uploads
names(req)
# Parses into a Rook multipart file type;needed for API conversions
fileInfo <- list(formContents = Rook::Multipart$parse(req))
# This is where the file name is stored
# print(fileInfo$formContents$req$filename)
file_name <- fileInfo$formContents$req$filename
# The file is downloaded in a temporary folder
tmpfile <- fileInfo$formContents$req$tempfile
# Create a file path
fn <- (paste0("data/images/",file_name, sepp=''))
#Copies the file into the designated folder
file.copy(tmpfile, fn)
res$body <- paste0("Your file is now stored in ", fn, "\n")
res
}