1

I have a text file saved in a directory inside the document directory I want to upload this file to stream (upload) to the server using Alamofire but I'm getting an error and I don't follow what is the error mean the response should be a string, the problem isn't in the format of the response data even if I used responseString I'm getting an error.

This my code:

 let folder = getFolder()

    let textFile = fileUrl?.appendingPathComponent(fileTemp)

    let headers: HTTPHeaders = [
    "Content-type": "multipart/form-data"
    ]

        let finalUrl = url + "/myMethod"

    Alamofire.upload(
    multipartFormData: { multipartFormData in
    multipartFormData.append(textFile!, withName: "file")

    },
    to: finalUrl,headers:headers,
    encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
    upload.responseJSON { response in
    debugPrint(response)
    }
    case .failure(let encodingError):
    print(encodingError)
    }})

And this is the response value I am getting:

Status Code: 400, Headers { "Cache-Control" = ( private ); "Content-Length" = ( 1647 ); "Content-Type" = ( "text/html" ); Date = ( "Thu, 06 Sep 2018 09:32:47 GMT" ); Server = ( "Microsoft-IIS/8.5" ); "X-AspNet-Version" = ( "4.0.30319" ); "X-Powered-By" = ( "ASP.NET" ); } } [Data]: 1647 bytes [Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 3." UserInfo={NSDebugDescription=Invalid value around character 3.}))

Does anyone have any idea about what I'm doing wrong? Thanks in advance!

kleusmeus
  • 61
  • 9
aoe
  • 87
  • 1
  • 1
  • 10

1 Answers1

0

you need to specify the upload format is text

Change this:

multipartFormData.append(textFile!, withName: "file")

To this:

multipartFormData.append(textFile!, withName: "file", fileName: filename, mimeType: "text/plain")
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
medics
  • 5
  • 3