【问题标题】:find the type of file fetched from HTTP URL in flutter在 Flutter 中查找从 HTTP URL 获取的文件类型
【发布时间】:2022-11-08 17:46:43
【问题描述】:

今天有人问我一个简单的问题,我想在这里回答这个问题可能会很好:

我想知道当我用HTTP获取文件时的内容类型,如何?

例如:

http.head(Uri.parse(myUrl)).then(
 (response) {
   if (response.statusCode == 200) {
     /*
         Now find the content type of myUrl
     */
   }
 },
);

【问题讨论】:

标签: flutter flutter-http


【解决方案1】:

正如我所说,这个问题很简单

如果您有这个问题,可以参考这个链接并使用official documents

但是如果你在 StackOverflow 中寻找这个问题的答案,答案是:

http.head(Uri.parse(myUrl)).then(
 (response) {
   if (response.statusCode == 200) {
      print(response.headers['content-type']);
   }
 },
);

编辑:

感谢jamesdlin 鉴于 content-type 是从服务器报告的值,它是不可靠的。 另一种更可靠的方法是使用package:mimelookupMimeType 函数。像这样:

http.get(Uri.parse(myUrl)).then(
 (response) {
   if (response.statusCode == 200) {
      final data = response.bodyBytes;
      final mime = lookupMimeType('', headerBytes: data);
      print(mime); // Print type of data
   }
 },
);

【讨论】:

  • value 是什么?
  • @Bijan 哎呀,我的意思是response.headers
猜你喜欢
  • 2021-09-12
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2021-09-18
  • 2021-02-23
  • 2021-10-13
相关资源
最近更新 更多