Flutter Uri.encodeFull()与Uri.encodeComponent()区别

说道区别我们先看一下官方的:

/**
   * Encode the string [component] using percent-encoding to make it
   * safe for literal use as a URI component.
   *
   * All characters except uppercase and lowercase letters, digits and
   * the characters `-_.!~*'()` are percent-encoded. This is the
   * set of characters specified in RFC 2396 and the which is
   * specified for the encodeUriComponent in ECMA-262 version 5.1.
   *
   * When manually encoding path segments or query components remember
   * to encode each part separately before building the path or query
   * string.
   *
   * For encoding the query part consider using
   * [encodeQueryComponent].
   *
   * To avoid the need for explicitly encoding use the [pathSegments]
   * and [queryParameters] optional named arguments when constructing
   * a [Uri].
   */
  static String encodeComponent(String component) 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`MultipartFile.fromFile` 是 Flutter 中的一个方法,用于创建一个 `MultipartFile` 对象来表示一个文件上传的数据。这个方法需要一个文件路径作为参数,它会读取指定路径的文件,并将该文件的内容转换为一个 `MultipartFile` 对象。 下面是一个示例代码,演示如何使用 `MultipartFile.fromFile` 方法: ```dart import 'dart:io'; import 'package:http/http.dart' as http; void uploadFile(String filePath) async { var file = File(filePath); var stream = http.ByteStream(file.openRead()); var length = await file.length(); var uri = Uri.parse('https://example.com/upload'); // 替换为实际的上传地址 var request = http.MultipartRequest('POST', uri); var multipartFile = http.MultipartFile('file', stream, length, filename: file.path.split('/').last); request.files.add(multipartFile); var response = await request.send(); if (response.statusCode == 200) { print('File uploaded successfully'); } else { print('Error uploading file'); } } ``` 在上面的例子中,我们首先创建了一个 `File` 对象来表示要上传的文件,然后使用 `file.openRead()` 方法获取文件的字节流,并使用 `file.length()` 方法获取文件的长度。 接下来,我们创建一个 `Uri` 对象来表示上传地址,并使用 `http.MultipartRequest` 创建一个 `http.Request` 对象。然后,我们使用 `MultipartFile.fromFile` 方法将文件转换为 `MultipartFile` 对象,并将其添加到请求中。 最后,我们使用 `request.send()` 方法发送请求,并根据响应的状态码判断上传是否成功。 请注意,上述示例中的上传地址和文件参数仅用作示例,请根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值