wxml :
<button bindtap="video">視訊上傳</button>
js:
video(){
wx.chooseVideo({
camera: 1,
success:(res)=>{
wx.uploadFile({
filePath: res.tempFilePath,
name: 'file',
url: 'http://www.cs.com/api/demo/index',
success:(res)=>{
console.log(res);
}
})
}
})
}
php
public function index(Request $request)
{
$pathName = ($request->file("file"))->getPathname();
// SECRETID和SECRETKEY請登入通路管理控制台進行檢視和管理
$secretId = ""; //"雲 API 密鑰 SecretId";
$secretKey = ""; //"雲 API 密鑰 SecretKey";
$region = "ap-shanghai"; //設定一個預設的存儲桶地域
$cosClient = new \Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //協定頭部,預設為http
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
$name=rand(111, 999) . time() . ".mp4";
### 上傳檔案流
try {
$bucket = "1811-1304504601"; //存儲桶名稱 格式:BucketName-APPID
$key = $name; //此處的 key 為對象鍵,對象鍵是對象在存儲桶中的唯一辨別
$srcPath = $pathName;//本地檔案絕對路徑
$file = fopen($srcPath, "rb");
if ($file) {
$result = $cosClient->putObject(array(
'Bucket' => $bucket,
'Key' => $key,
'Body' => $file));
// return json_decode($result, true);
$video="http://cdn加速域名/{$name}";
return json(["error_code"=>0,"data"=>$video,"msg"=>"上傳成功"]);
}
} catch (\Exception $e) {
return json(["error_code"=>1,"data"=>$e,"msg"=>"失敗"]);
}
}
傳回的路徑是cdn加速域名拼上視訊名,加防盜鍊可能會有影響