#鸿蒙通关秘籍#如何使用HarmonyOS NEXT上传文件到服务器?

HarmonyOS
2024-12-05 15:52:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Yvr第七章SCM

要实现文件上传,构建HTTP请求的body内容至关重要。通过以下方法,可以生成上传文本文件的请求体:

buildBodyContent(boundary: string, fileName: string, content: string) {
    let body = `--${boundary}\r\n`
    body += `Content-Disposition: form-data; name="file"; filename="${fileName}"\r\n`
    body += `Content-Type: text/plain\r\n`
    body += '\r\n'
    body += content
    body += '\r\n'
    body += `--${boundary}--\r\n`
    return body
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

确保Content-Disposition和Content-Type设置正确,同时boundary参数不能与文件内容重复。

分享
微博
QQ
微信
回复
2024-12-05 16:41:03


相关问题
HarmonyOS 上传图片服务器
662浏览 • 1回复 待解决
HarmonyOS 文件上传服务器问题
1147浏览 • 1回复 待解决
HarmonyOS Rcp.post上传图库文件服务器
1508浏览 • 1回复 待解决
HarmonyOS 选择图片上传服务器的demo
901浏览 • 1回复 待解决
HarmonyOS 将相册中的图片上传服务器
1607浏览 • 1回复 待解决