Search code examples
iosswiftnsurlsessionoutputstreamurlsession

iOS: Problem to send big files (more than 3Gb) with OutputStream(url: , append:) => Code Status 500


I try to send big video files with the OutputStream(url: payloadFileURL, append:) function but I have a code status 500. (I precise that when I send 1gb, 2gb it works like a charm)

Here is the entire code: Send big files with OutputStream

private func buildPayloadFile(videoFileURL: URL, boundary: String, fileName: String, eventId: Int, contactId: Int, type: Int) throws -> URL {
    let mimetype = "video/mp4"

    let payloadFileURL = URL(fileURLWithPath: NSTemporaryDirectory())
        .appendingPathComponent(UUID().uuidString)

    guard let stream = OutputStream(url: payloadFileURL, append: false) else {
        throw UploadError.unableToOpenPayload(payloadFileURL)
    }

    stream.open()

    //define the data post parameter
    stream.write("--\(boundary)\r\n")
    stream.write("Content-Disposition:form-data; name=\"eventId\"\r\n\r\n")
    stream.write("\(eventId)\r\n")

    stream.write("--\(boundary)\r\n")
    stream.write("Content-Disposition:form-data; name=\"contactId\"\r\n\r\n")
    stream.write("\(contactId)\r\n")

    stream.write("--\(boundary)\r\n")
    stream.write("Content-Disposition:form-data; name=\"type\"\r\n\r\n")
    stream.write("\(type)\r\n")

    stream.write("--\(boundary)\r\n")
    stream.write("Content-Disposition:form-data; name=\"file\"; filename=\"\(fileName)\"\r\n")
    stream.write("Content-Type: \(mimetype)\r\n\r\n")
    if stream.append(contentsOf: videoFileURL) < 0 {
        throw UploadError.unableToOpenVideo(videoFileURL)
    }
    stream.write("\r\n")

    stream.write("--\(boundary)--\r\n")
    stream.close()

    return payloadFileURL
}

Any ideas?


Solution

  • Error 500 means a server side problems. There can be several reasons:
    - server side configuration
    - server disk space
    - problem with server implementation

    Please also check your client code:
    - Content Type
    - Mime type
    - Enough space on device. Because of this big file, it will need more space on the device to store in memory during "cache"