我使用来自android客户端的改型发送了图像文件和内容(文本),并试图在服务器端获取它们,但不能这样做,并且错误始终是:它为null。 请问如何使用#Retrofit和Slim框架发送和接收图像? 如果有人可以提供帮助,我将不胜感激。
..................................... 试图像多部分文件一样从android客户端发送图像,并使用苗条的使用方法(getUploadedFiles)接收图像,但它不起作用。
@Multipart
@POST("createPostWithImage")
Call<DefaultResponse> uploadTestPost(
@Part("desc") RequestBody desc,
@Part MultipartBody.Part image
);
$app-> post('/createPostWithImage', function(Request $request, Response $response) {
$directory = __DIR__.'../photos/1';
$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['photo'];
$uploadedFile->moveTo($directory);
});
获取从android客户端发送的文件,并将其保存到特定文件夹中。
答案 0 :(得分:0)
$app->post('/file', function($request,$response) {
try{
$directory = $this->get('upload_directory');
$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['example1'];//id of html type=file <no form needed>
$extension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$basename = mt_rand(10000000, 99999999);
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $basename.'.'.$extension);return $response->withJson(array('message' =>$uploadedFile),200);
catch(\Exception $ex){
return $response->withJson(array('error' => $ex->getMessage()),422);
} });
minimal code to get started ...