Suryakant Sharma
Suryakant Sharma

Reputation: 3960

File getting corrupted if user cancels file UPLOAD on WEBDAV repository

In one of my iOS app, I'm trying to upload a file on WEBDAV repository and this is how I'm preparing things for UPLOAD -

func createUploadRequest(url: String) -> URLRequest? {
    if let url = URL(string: url) {
        var request = URLRequest(url:url)
        request.httpMethod = "PUT"
        request.setValue("1", forHTTPHeaderField: "Depth")
        request.setValue("application/xml", forHTTPHeaderField: "Content-Type")
        return request
    }
    return nil
}

@IBAction func upload() {
    if let request = createUploadRequest(url: url) {
        let sessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "sessionIdentifier")
        self.session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
        uploadTask = self.session?.uploadTask(with: request as URLRequest, fromFile: self.fileUrl!)
        self.uploadTask?.resume()
    }

}

When upload is in progress and I cancel the uploadtask as follows

@IBAction func cancelUpload() {
    self.uploadTask?.cancel()
}

The upload process gets canceled but the problem here is the file is getting corrupted on WEBDAV Server, which is being uploaded and uploadtask is canceled in between.

Strangely this is working fine in iOS 9 and upload process getting canceled properly without file being corrupted but iOS version 12 and above file getting corrupted. I didn't check the behaviour on iOS 10 or 11 though.

Please help me get this issue resolved. Thank you in advance.

Please note that this bug persists with/without lock on WEBDAV resource

Upvotes: 1

Views: 136

Answers (0)

Related Questions