我是Swift Programming和FireBase的新手,我已经实现了聊天应用程序,我只能上传图像和视频,它的工作正常。现在我想上传像Docx,txt,pdf这样的文件,我已经冲了很多,但是我从来没有找到任何解决方案,他们也可以上传文件,任何人都可以建议我的解决方案。
这里有一些上传和下载图片和视频的代码
//Code to send Images and Videos
// MARK: - Send media message
func sendMedia(message : MSMessage, senderRef : FIRDatabaseReference,image : Data?, video : URL? ){
let image_videos: String?
if image != nil{
image_videos = "Send you a Photo"
let uniqeID : String = "\(NSUUID().uuidString).jpg"
// save photo in firebase storage
DBManager.manager.imageStorageRef.child(Constants.USERS_IMAGES_STORAGE)
.child(message.senderId).child(uniqeID)
.put(image!, metadata: nil){
(metadata : FIRStorageMetadata?, err : Error?) in
// problem with upload image
if err != nil{ // Error occurred, infrom the user
self.delegate?.errorOccurred(description: (err?.localizedDescription)!)
}else{// send link to photo in user messages
let url = String(describing: metadata!.downloadURL()!)
self.sendMessage(message: message, senderRef: senderRef, url : url, fileName : uniqeID)
}
}// dbmanager
}else{
let uniqeID : String = NSUUID().uuidString
image_videos = "Send you a Video"
// save video in firebase storage
DBManager.manager.videoStorageRef.child(Constants.USERS_VIDEOS_STORAGE)
.child(message.senderId).child(uniqeID).putFile(video!, metadata : nil ){
(metadata : FIRStorageMetadata?, err : Error?) in
// problem with upload video
if err != nil{ // Error occurred, infrom the user
self.delegate?.errorOccurred(description: (err?.localizedDescription)!)
}else{// send link to video in user messages
let url = String(describing: metadata!.downloadURL()!)
self.sendMessage(message: message, senderRef: senderRef, url : url, fileName : uniqeID)
}
}
} }
//Code to receive Images and videos
// MARK : - Observe Handler
func observeHandler(snapshot : FIRDataSnapshot) {
guard let data = snapshot.value as? NSDictionary else{
return
}
// check if there is data
guard let senderID = data[Constants.SENDER_ID] as? String,
let senderName = data[Constants.SENDER_NAME] as? String,
let sentDate = data[Constants.SENT_DATE] as? Int,
let text = data[Constants.SENDER_TEXT] as? String,
let receiverID = data[Constants.RECEIVER_ID] as? String,
let fileURL = data[Constants.MEDIA_URL] as? String ,
let fileName = data[Constants.FILE_NAME] as? String,
let messageID = data[Constants.MESSAGE_ID] as? String else{
debugPrint("\(#function ) error return")
return
}
if !text.isEmpty{ //Text message
let ReceiveID = UserDefaults.standard.string(forKey: "ReceiveID")
if AuthManager.User.id.value == receiverID && ReceiveID == senderID {
// create new message object
let newMessage = MSMessage(senderId: senderID,
senderDisplayName: senderName,
date: self.millisToDate(sentDate), text: text,
receiverID : receiverID,
messageID: messageID)
// notify caller
self.delegate?.messageReceived(message : newMessage, fromNode: false)
}
}else if !fileURL.isEmpty{ // Media message
guard let mediaURL = URL(string: fileURL) else{
return
}
// If the media is Image
if self.isImage(fileURL){
self.getImage(mediaURL,senderID,senderName,receiverID,sentDate,messageID, fileName: fileName)
}else{ // Media is video
self.getVideo(mediaURL,senderID,senderName,receiverID,sentDate,messageID, fileName: fileName)
}
}
}
// MARK: - Get video
func getVideo(_ mediaURL : URL,_ senderID : String,_ senderName : String,_ receiverID : String,
_ sentDate : Int, _ messageID : String , fileName : String,completion: ((MSMessage)->())? = nil){
DispatchQueue.global().sync {
let video = JSQVideoMediaItem(maskAsOutgoing: senderID == AuthManager.User.id.value ? true : false)
video?.fileURL = mediaURL
video?.isReadyToPlay = true
let newMessage = MSMessage(senderId: senderID,
displayName: senderName,
media: video!,
date: (self.millisToDate(sentDate)),
receiverID: receiverID,
messageID: messageID,
mediaURL : mediaURL,
fileName : fileName)
DispatchQueue.main.async {
if completion != nil{
completion!(newMessage)
}else{
self.delegate?.mediaReceived(message: newMessage)
}
}
}
}
答案 0 :(得分:0)
这是代码
// Get a reference to the storage service using the default Firebase App
let storage = Storage.storage()
// Create a storage reference from our storage service
let storageRef = storage.reference()
// File located on disk
let localFile = URL(string: "path/to/docs/rivers.pdf")!
// Create a reference to the file you want to upload
let riversRef = storageRef.child("docs/rivers.pdf")
// Upload the file to the path "docs/rivers.pdf"
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
storageRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
有关详细信息,请参阅此处的firebase iOS文档: https://firebase.google.com/docs/storage/ios/upload-files