Post

Replies

Boosts

Views

Activity

Need to upload multipart form data( video, image with params) in background until upload is not finished no matter app is in background or switched to other app or terminated.
This Code is not working. Any Help will be appreciated. func UPLOAD_VIDEO_WITH_DATA(_ video: [UploadImageInfo], param: [String: Any]) { if !APIManager.isConnectedToNetwork() { APIManager().networkErrorMsg() return } let url = URL(string: API.CAMPAIGN.create)! var request = URLRequest(url: url) let boundary = "Boundary-\(NSUUID().uuidString)" var body = Data() body.append(boundary) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"params\"\r\n") body.append("Content-Type: application/json\r\n\r\n") body.append("\(param)") body.append("\r\n") let tempDirectoryURL = FileManager.default.temporaryDirectory let videoURL = tempDirectoryURL.appendingPathComponent("video.mp4") for videoInfo in video { if videoInfo.video != nil { if let videoData = videoInfo.data { try? videoData.write(to: videoURL) body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"video\"; filename=\"\(videoInfo.video?.absoluteString)\"\r\n") body.append("Content-Type: video/mp4\r\n\r\n") body.append(videoData) body.append("\r\n") } } } body.append("--\(boundary)--\r\n") request.httpMethod = "POST" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(self.getMultipartHeaderWithToken())", forHTTPHeaderField: "Authorization") request.httpBody = body print(request) let session = URLSession(configuration: URLSessionConfiguration.background(withIdentifier: "com.app.Deloa.backgroundUpload")) DispatchQueue.global().async { let task = session.uploadTask(with: request, fromFile: videoURL) task.resume() } } public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if let error = error { print("Error uploading data: \(error)") } else { print("Data uploaded successfully!") } } extension Data { mutating func append(_ string: String) { if let data = string.data(using: .utf8) { append(data) } } } Here is the data i am passing for video ` override func selectedVideo(choosenVideo: URL, thumbnailImage: UIImage) { var selectedVideo = UploadImageInfo() selectedVideo.name = "video" selectedVideo.video = choosenVideo selectedVideo.data = try? Data(contentsOf: choosenVideo) selectedVideo.url = choosenVideo.absoluteString self.media.append(selectedVideo) var selectedThumbnail = UploadImageInfo() selectedThumbnail.name = "thumbnail" selectedThumbnail.image = thumbnailImage selectedThumbnail.data = compressImage(image: thumbnailImage) self.media.append(selectedThumbnail) }` Upload Image struct struct UploadImageInfo { var id: Int var name: String var image: UIImage? var video : URL? var thumbnail: UIImage? var data: Data? var url: String? init() { name = "" image = nil video = nil id = 0 thumbnail = nil data = nil url = nil } }
0
0
803
Jul ’23