Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Upload files from web #10

Closed
Mythar opened this issue Oct 7, 2021 · 1 comment
Closed

Upload files from web #10

Mythar opened this issue Oct 7, 2021 · 1 comment

Comments

@Mythar
Copy link

Mythar commented Oct 7, 2021

Took some digging to figure out, here is my code, hope this helps others :)


// Import
import 'dart:async';
import 'package:path/path.dart';
import 'package:dio_http/dio_http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:http/http.dart' as http;
import 'package:mime_type/mime_type.dart';

// multipartFileFromNetwork
Future<MultipartFile> multipartFileFromNetwork(String path) async {
  try {
    http.Response response = await http.get(Uri.parse(path));
    if ((response.statusCode > 199) && (response.statusCode < 300)) {
      String fileName = basename(path);
      String ext = extension(fileName);

      if (ext == '') {
        ext = extensionFromMime(response.headers['content-type']);
        fileName = fileName + '.' + ext;
      }

      return MultipartFile.fromBytes(
          response.bodyBytes,
          filename: fileName,
          contentType: MediaType.parse(response.headers['content-type'])
      );
      
    } else {
      return null;
    }
  } catch (e) {
    return null;
  }
}

@jgoyvaerts
Copy link
Contributor

Hey, thanks for the sample but this is not the place for posting sample code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants