laravel use Postman Upload multiple images

Postman Post request uploaded files

A selected post request mode, the address input request

Second, fill in Headers

Key:Content-Type

Value:multipart/form-data

[{"key":"Content-Type","value":"multipart/form-data","description":"","enabled":true}]

As shown below

Third, fill in the body

Form-data selection

Then select the file file

Click Add File, and finally sent to

When written with Laravel interface, I do not know how to test how postmam test with multi-map upload, find a lot of information did not understand, the general data are written to upload a single picture of the demonstration, hereby recorded.

Write Laravel background interface

The album was a write uploaded to the server code, I Caishuxueqian, please correct me if wrong

/**
	 * 上传相册到服务器,同时相关数据存入数据库
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function store(Request $request)
	{
		$data = [
			'uid' => $request->input('uid'),
			'albums' => []
		];
		$pathUrls = [];

		if($request->has('images') )
		{
			$images = $request->file('images');
			$content = $request->input('content');

			//适配单文件和多文件上传
			if(is_array($images))
			{
				foreach($images as $key=>$v)
				{
					$path = $images[$key]->store('images','public');
					$path =  Storage::disk('public')->url($path);
					array_push($pathUrls,$path);

				}
			} else {
				$images->store('images','public');
				$path =  Storage::disk('public')->url($images);
				array_push($pathUrls,$path);
			}

			$pathUrls = implode(',',$pathUrls);

			$album = Album::create([
				'user_id' => $data['uid'],
				'content' => $content,
				'photos_url' => $pathUrls
			]);

			$album->save();
			$data['albums']['id'] = $album->id;
			$data['albums']['images'] = explode(',', $pathUrls) ;
			$data['albums']['content'] = $content;
		}


		return response()->json([
			'status' => 'success',
			'status_code' =>200,
			'data' => $data,
		]);

	}

==== ======= my supplementary

1.Key:Content-Type

Value:multipart/form-data

2 field is used in the form of an array

Reference: https://blog.csdn.net/hl449006540/article/details/85015782

Reference: https: //blog.csdn.net/maowendi/article/details/80537304

Released 2395 original articles · won praise 53 · views 440 000 +

Guess you like

Origin blog.csdn.net/lxw1844912514/article/details/105395550