无法将视频从Flutter上传到服务器php?

时间:2019-07-14 14:42:54

标签: php flutter

我尝试上传视频。收到消息“视频已上传”,在“上传”文件夹中未收到视频文件。当我尝试上载图像文件时,它可以工作(上载的图像文件已到达“上载”文件夹中)。我的代码不适用于视频文件。如何使其接收视频文件?

这是一个飞镖文件。

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Upload MySQL',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  File _video;

  Future getVideoGallery() async{
    var imageFile = await ImagePicker.pickVideo(source: ImageSource.gallery);

    setState(() {
      _video = imageFile;
    });
  }

  Future getVideoCamera() async{
    var imageFile = await ImagePicker.pickVideo(source: ImageSource.camera);
            setState(() {
      _video = imageFile;
    });
  }

  Future uploadVideo(File videoFile) async{
    var uri = Uri.parse("http://192.168.1.1/ABC/uploadVideo.php");
    var request = new MultipartRequest("POST", uri);

    var multipartFile = await MultipartFile.fromPath("video", videoFile.path);
    request.files.add(multipartFile);
    StreamedResponse response = await request.send();
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });
    if(response.statusCode==200){
      print("Video uploaded");
    }else{
      print("Video upload failed");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Upload Image"),),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
           _video==null
              ? new Text("No video selected!")
              : new Text("video is selected"),
            Row(
              children: <Widget>[
                RaisedButton(
                  child: Icon(Icons.video_library),
                  onPressed: getVideoGallery,
                ),
                RaisedButton(
                  child: Icon(Icons.videocam),
                  onPressed: getVideoCamera,
                ),
                RaisedButton(
                  child: Text("UPLOAD video"),
                  onPressed:(){
                    uploadVideo(_video);
                  },
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

这是php文件。

<?php

include 'conn.php';

$video=$_FILES['video']['name'];
$videoPath="uploads/".$video;
move_uploaded_file($_FILES['video']['tmp_name'],$videoPath);

?>

2 个答案:

答案 0 :(得分:0)

好的,最后,我找到了解决方案。那是为了增加php.ini中的上传限制。 我可以上传图片,但不能上传视频,因为视频文件大于最大上传限制的2MB。

答案 1 :(得分:0)

我在使用 HTTP 库时遇到了这个问题 您可以改用 Dio 包。 它支持大文件,对我来说效果很好。

sendVideoDio(String kMainUrl, XFile video) async {
String filePath = video.path;
String fileName = 'any name';

try {
  FormData formData = FormData.fromMap({
    "file":
    await MultipartFile.fromFile(filePath, filename:fileName),
  });
  Response response =
  await Dio().post(kMainUrl, data: formData);
  print("File upload response: $response");
  print(response.data['message']);
} catch (e) {
  print("Exception Caught: $e");
}

}