【问题标题】:Read CSV file sent to Laravel route读取发送到 Laravel 路由的 CSV 文件
【发布时间】:2021-05-10 09:12:42
【问题描述】:

我正在通过 curl 向 Laravel 中处理的端点发送一个 CSV 文件。

curl -X POST -H 'Content-Type: text/csv' -d "prices=@${file}" 'http://www.thissite.com/getprices' 

路线是

Route::post('getprices', [
    'uses'       => 'PriceController@getPrices',
]);

但我不确定如何打开和读取文件。功能是

public function getPrices(Request $request)
{
   $file = $request->file('prices');
    
   \Log::info(print_r($file->extension(), true));

    return $this->response([], Response::HTTP_OK);
}

我也试过

public function getPrices()
{
   $file = $this->getRequest()->file('prices');
    
   \Log::info(print_r($file->extension(), true));

    return $this->response([], Response::HTTP_OK);
}

但没有一个工作。 $file 为空。有人可以帮我从这里出去吗。我一直无法找到有关如何通过 laravel 路由接收 csv 文件的任何信息。

感谢您的帮助。

【问题讨论】:

  • 我不确定它是否可以一次性完成,我认为您需要单独的函数来保存上传的文件,然后从本地文件系统中检索它以读取它
  • 我仍然可以将文件发送到 Laravel 路由进行保存吗?如果可以,我可以使用作业来处理文件。

标签: php laravel csv file


【解决方案1】:

这个解决方案有效

卷曲请求:

curl -F "prices=@${file}" 'http://www.thissite.com/getprices' 

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2017-09-21
    • 1970-01-01
    • 2013-01-25
    相关资源
    最近更新 更多