【问题标题】:Uploading text file using Alamofire使用 Alamofire 上传文本文件
【发布时间】:2018-09-06 09:45:51
【问题描述】:

我有一个文本文件保存在文档目录内的目录中 我想使用 Alamofire 将此文件上传到流式传输(上传)到服务器,但我收到一个错误,我不明白错误是什么意思 响应应该是一个字符串,问题不在于响应数据的格式,即使我使用了 responseString 我得到了一个错误。

这是我的代码:

 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)
    }})

这是我得到的响应值:

状态代码:400,标题 { “缓存控制” = ( 私人的 ); “内容长度” = ( 1647 ); “内容类型” = ( “文本/html” ); 日期 = ( “格林威治标准时间 2018 年 9 月 6 日星期四 09:32:47” ); 服务器 = ( “微软-IIS/8.5” ); "X-AspNet-版本" = ( “4.0.30319” ); “X-Powered-By”=( “ASP.NET” ); } } [数据]:1647字节[结果]:失败:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误 Domain=NSCocoaErrorDomain Code=3840 "字符周围的值无效 3." UserInfo={NSDebugDescription=字符 3.}))

有人知道我做错了什么吗? 提前致谢!

【问题讨论】:

标签: ios upload swift4 alamofire multipart


【解决方案1】:

需要指定上传格式为文本

改变这个:

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

至此:

multipartFormData.append(textFile!, withName: "file", fileName: filename, mimeType: "text/plain")

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2019-12-06
    • 2016-06-02
    • 1970-01-01
    • 2019-04-19
    • 2017-03-16
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多