颤振:如何通过视频将数据传递到我的php页面
我需要通过我想要上传的视频传递数据,但我不知道如何做,就像这些数据一样,我需要通过视频将其传递到php页面颤振:如何通过视频将数据传递到我的php页面,php,flutter,dart,Php,Flutter,Dart,我需要通过我想要上传的视频传递数据,但我不知道如何做,就像这些数据一样,我需要通过视频将其传递到php页面 var data = { "fileInput": videoFile.path, "titleInput": titleController.text, "descriptionInput": descriptionController.text, "privacyInp
var data = {
"fileInput": videoFile.path,
"titleInput": titleController.text,
"descriptionInput": descriptionController.text,
"privacyInput": selectedCategorise.value,
"categoryInput": listcategory.map((e) => {e['name']}),
};
我知道如何在没有像这样的视频的情况下传递它。我传递电子邮件和密码
`signin() async {
var formdata = formstatesignin.currentState;
if (formdata.validate()) {
formdata.save();
var data = {"email": email.text, "password": password.text};
var url = "http://10.0.2.2/mobtech2/login.php";
var response = await http.post(url, body: data);
var responsebody = jsonDecode(response.body);
if (responsebody['status'] == "success") {
savePref(responsebody['username'], responsebody['email'],
responsebody['id']);
Navigator.of(context).pushNamed("homepage");
} else {
Navigator.of(context).pop();
}
} else {
print("Not vaild");
}
}`
这是我想要传递数据给它的函数
`Future uploadVideo(File videoFile) async {
var uri = Uri.parse("http://10.0.2.2/videoTube/file_upload.php");
var request = new http.MultipartRequest("POST", uri);
var multipartFile =
await http.MultipartFile.fromPath("video", videoFile.path);
request.files.add(multipartFile);
http.StreamedResponse response = await request.send();
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
//final response = await http.post(uri, body: data);
if (response.statusCode == 200) {
print("Video uploaded");
} else {
print("Video upload failed");
}
}`
此代码用于php中的类处理
`<?php
include "config.php";
if (!isset($_POST["video"])) {
echo "No video sent to page";
exit();
}
// 1) create file Uplaod Data
$videoUploadData = new VideoUploadData(
$_FILES["video"],
$_POST["titleInput"],
$_POST["descriptionInput"],
$_POST["privacyInput"],
$_POST["categoryInput"],
"REPLACE-THIS"
);
// 2) Process video data (upload)
$videoProcessor = new VideoProcessor($con);
$wasSuccessful = $videoProcessor->upload($videoUploadData);
?>`
`是的,您可以上传一段带有名称和标题等描述的视频
这是一个示例,我使用MultipartFile将带有一些描述的视频上载到我的服务器后端
当您使用函数_upload(video1)时,我将视频作为参数传递,但如果您愿意,可以通过其他方式传递。我还为视频上传进度设置了进度指示器
// create listing,post and upload video to the server
void _upload(video1) async {
// get variables from shared preferences
token = await SharedPreferencesProvider.getUserToken();
userId = await SharedPreferencesProvider.getUserId();
// api to upload video
// Post a video to
var url = 'https://example.com/api/v1/video';
var video1Path;
if (video1 == null) {
return;
} else {
video1Path = await MultipartFile.fromFile(video1.path);
}
FormData data = FormData.fromMap({
"title": title,
"products": "1",
"description": description,
"category_id": categoryId,
"category_name": categoryName,
"longitude": _longitude,
"latitude": _latitude,
"video1": video1Path,
});
Dio dio = new Dio();
dio.options.headers["Authorization"] = "Bearer $token";
dio.post(url, data: data, onSendProgress: (int sent, int total) {
setState(() {
_isUploading = true;
// show a progress %
progressString ='uploading :' + ((sent / total) * 100).toStringAsFixed(0) + "%";
if ( ((sent / total) * 100).toStringAsFixed(0) == '100'){
progressString = 'Saving...';
}
//progressString = ((sent / total) * 100).toStringAsFixed(0) + "%";
});
}).then((response) async => {
setState(() {
progressString = DemoLocalizations.of(context).uploadCompleted;
}),
print(response),
//after successful product post we redirect to(Main page)
goHome()
})
.catchError((error) => print(error));
}
你是说你想用PHP后端上传一个视频文件到服务器上吗?我实际上知道如何用PHP作为后端上传一个视频到服务器上,但也知道如何在同一个功能中上传其他数据,例如(标题、描述、类别和隐私)
`<?php
class VideoProcessor{
private $con;
private $sizeLimit = 500000000;
private $allowedTypes = array("mp4", "flv", "webm", "mkv", "vob", "ogv", "ogg", "avi", "wmv", "mov", "mpeg", "mpg");
public function __construct($con) {
$this->con = $con;
}
public function upload($videoUploadData) {
$targetDir = "uploads/videos/";
$videoData = $videoUploadData->videoDataArray;
$tempFilePath = $targetDir . uniqid() . basename($videoData["name"]);
$tempFilePath = str_replace(" ", "_", $tempFilePath);
$isValidData = $this->processData($videoData, $tempFilePath);
if (!$isValidData) {
return false;
}
if (move_uploaded_file($videoData["video"]["tmp_name"], $tempFilePath)) {
$finalFilePath = $targetDir . uniqid() . ".mp4";
if (!$this->insertVideoData($videoUploadData, $finalFilePath)) {
echo "Insert query failed";
return false;
}
}
}
private function processData($videoData, $filePath) {
$videoType = pathInfo($filePath, PATHINFO_EXTENSION);
if (!$this->isValidSize($videoData)) {
echo "File too large. Can't be more than" . $this->sizeLimit . " bytes";
return false;
}
else if (!$this->isValidType($videoType)) {
echo "Invalid file type";
return false;
}
else if ($this->hasError($videoData)) {
echo "Error code: " . $videoData["error"];
return false;
}
return true;
}
private function isValidSize($data) {
return $data["size"] <= $this->sizeLimit;
}
private function isValidType($type) {
$lowercased = strtolower($type);
return in_array($lowercased, $this->allowedTypes);
}
private function hasError($data) {
return $data["error"] != 0;
}
private function insertVideoData($uploadData, $filePath){}
}
?>`
// create listing,post and upload video to the server
void _upload(video1) async {
// get variables from shared preferences
token = await SharedPreferencesProvider.getUserToken();
userId = await SharedPreferencesProvider.getUserId();
// api to upload video
// Post a video to
var url = 'https://example.com/api/v1/video';
var video1Path;
if (video1 == null) {
return;
} else {
video1Path = await MultipartFile.fromFile(video1.path);
}
FormData data = FormData.fromMap({
"title": title,
"products": "1",
"description": description,
"category_id": categoryId,
"category_name": categoryName,
"longitude": _longitude,
"latitude": _latitude,
"video1": video1Path,
});
Dio dio = new Dio();
dio.options.headers["Authorization"] = "Bearer $token";
dio.post(url, data: data, onSendProgress: (int sent, int total) {
setState(() {
_isUploading = true;
// show a progress %
progressString ='uploading :' + ((sent / total) * 100).toStringAsFixed(0) + "%";
if ( ((sent / total) * 100).toStringAsFixed(0) == '100'){
progressString = 'Saving...';
}
//progressString = ((sent / total) * 100).toStringAsFixed(0) + "%";
});
}).then((response) async => {
setState(() {
progressString = DemoLocalizations.of(context).uploadCompleted;
}),
print(response),
//after successful product post we redirect to(Main page)
goHome()
})
.catchError((error) => print(error));
}