ios - 使用 twilio 可编程聊天 API 发送 PDF/文本
问题描述
我正在使用适用于 iOS 的 Twilio 可编程聊天 SDK 创建一个聊天应用程序。我按照这个文档使用 SDK 发送图像、文本,它工作正常。
但是当我尝试使用内容类型应用程序/pdf 或文本发送 pdf 时,它会引发以下错误。
Error Domain=signal.sdk.domain.error Code=101 "" UserInfo={kTCHErrorMsgKey=, NSLocalizedDescription=}
下面是我用来发送pdf的代码,
do {
let fileData = try Data(contentsOf: pickedDocUrl)
// Prepare the upload stream and parameters
let messageOptions = TCHMessageOptions()
let inputStream = InputStream(data: fileData)
messageOptions.withMediaStream(inputStream,
contentType: "application/pdf",
defaultFilename: "\(pickedDocUrl.lastPathComponent.components(separatedBy: ".")[0]).pdf",
onStarted: {
// Called when upload of media begins.
print("Media upload started")
},
onProgress: { (bytes) in
// Called as upload progresses, with the current byte count.
print("Media upload progress: \(bytes)")
}) { (mediaSid) in
// Called when upload is completed, with the new mediaSid if successful.
// Full failure details will be provided through sendMessage's completion.
print("Media upload completed")
}
// Trigger the sending of the message.
self.generalChannel?.messages?.sendMessage(with: messageOptions,
completion: { (result, message) in
self.pickedDocUrl = nil
if !result.isSuccessful() {
print("Creation failed: \(String(describing: result.error))")
} else {
print("Creation successful")
}
})
} catch {
print("Unable to load data: \(error)")
}
解决方案
我有同样的问题..通过从文件中添加 MIME 或内容类型来修复它
let mimeType = mimeTypeForPath(path: fileUrl.path)
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream";
}
推荐阅读
- web - 有没有办法将 IP 摄像机流嵌入网站?
- python - 如何使用 flask_msearch 配置模糊性等参数?
- bash - 如何按“块”文本拆分解析的文本?
- r - 上传到 Shiny 并将其分配给环境以供进一步使用
- c# - Puppeteer 中 LaunchAsync() 的线程安全
- c# - 基于下面的哈希方法写一个UnHash方法
- c# - 引用同一列 NHibernate 的两个多对一关系
- react-hooks - 使用 React 测试 useSubscription apollo hooks
- azure-active-directory - Get information about invitation of B2B user in Microsoft Graph
- node.js - 通过在nodejs中创建单独的http服务器来监听媒体(图像/视频)是否更好?