Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation needed for using withReadStream enabled. #771

Closed
dibyasom opened this issue Jul 16, 2021 · 3 comments
Closed

Documentation needed for using withReadStream enabled. #771

dibyasom opened this issue Jul 16, 2021 · 3 comments
Labels
documentation suggestion New feature or request

Comments

@dibyasom
Copy link

Some documentation to show how to use file_picker withReadStream set would be helpful, for working/uploading big files.

@dibyasom dibyasom added the suggestion New feature or request label Jul 16, 2021
@miguelpruivo
Copy link
Owner

Yes, I might add one.

@redsolver
Copy link
Contributor

@vhaegar1526 You can pick and upload a large video file using this code for example:

import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

void main() async {
  final result = await FilePicker.platform.pickFiles(
    type: FileType.video,
    allowedExtensions: [
      'mp4',
      'webm',
    ],
    withData: false,
    withReadStream: true,
  );

  if (result != null) {
    final file = result.files.first;

    var uri = Uri.https('siasky.net', '/skynet/skyfile');

    var stream = http.ByteStream(file.readStream);

    var request = http.MultipartRequest('POST', uri);

    final mimeType = lookupMimeType(file.filename ?? '');

    var multipartFile = http.MultipartFile(
      'file',
      stream,
      file.size,
      filename: file.filename,
      contentType: mimeType == null ? null : MediaType.parse(mimeType),
    );

    request.files.add(multipartFile);

    final httpClient = http.Client();
    final response = await httpClient.send(request);

    if (response.statusCode != 200) {
      throw Exception('HTTP ${response.statusCode}');
    }

    final body = await response.stream.transform(utf8.decoder).join();

    print(body);
  }
}

@miguelpruivo
Copy link
Owner

Added to the docs. Thank you @redsolver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation suggestion New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants