iOS Swift使用Alamofire上传PDF文件(多部分) [英] iOS Swift uploading PDF file with Alamofire (Multipart)

查看:567
本文介绍了iOS Swift使用Alamofire上传PDF文件(多部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用iOS 10,Swift 3和Alamofire 4开发应用程序

I'm currently developing an application using iOS 10 and Swift 3 and Alamofire 4

此应用程序的目的是上传先前生成的PDF文件.

The purpose of this application is to upload a PDF file generated previously.

PDF生成工作正常,并且文件已创建.

The PDF generation is working perfectly and the file is created.

但是上传不起作用… 我收到成功回复,但文件未上传.

However the upload doesn’t work…
 I received a success response but the file is not uploaded.

我的服务器回复

Multi part Content-Type => multipart/form-data; boundary=alamofire.boundary.56958be35bdb49cb
Multi part Content-Length => 293107
Multi part Content-Boundary => alamofire.boundary.56958be35bdb49cb
 responses 
SUCCESS: {
    uploadedFiles =     (
                {
            details = " Key=Content-Disposition - values=[form-data; name=\"pdfDocuments\"] length=8";
            storedFileName = "/var/www/pdf/17/009/22/TMP104150531290406.tmp";
            type = PDF;
            uploadedDate = 1483999296701;
            uploadedFileName = UnknownFile;
        }
    );
}
end responses

我正在使用多部分程序将我的文件作为数据上传,如您所见

I’m using multi-part to upload my file as Data as you can see here

文件url可以.

我已经搜索过SO,但找不到任何有效的解决方案……

I have searched on SO but didn’t find any solution working…

在这里您可以看到我的控制器

Here you can see my Controller

Alamofire.upload(
            multipartFormData: {
                multipartFormData in

                if let urlString = urlBase2 {
                    let pdfData = try! Data(contentsOf: urlString.asURL())
                    var data : Data = pdfData

                    multipartFormData.append(data as Data, withName:"test.pdf", mimeType:"application/pdf")
                    for (key, value) in body {
                        multipartFormData.append(((value as? String)?.data(using: .utf8))!, withName: key)
                    }

                    print("Multi part Content -Type")
                    print(multipartFormData.contentType)
                    print("Multi part FIN ")
                    print("Multi part Content-Length")
                    print(multipartFormData.contentLength)
                    print("Multi part Content-Boundary")
                    print(multipartFormData.boundary)
                }
        },
            to: url,
            method: .post,
            headers: header,
            encodingCompletion: { encodingResult in

                switch encodingResult {

                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print(" responses ")
                        print(response)
                        print("end responses")

                        onCompletion(true, "Something bad happen...", 200)

                    }
                case .failure(let encodingError):
                    print(encodingError)
                    onCompletion(false, "Something bad happen...", 200)
                }
        })

预先感谢您的帮助.

致谢

推荐答案

我刚刚找到解决此错误的解决方案.

I have just found my solution to fix this bug.

我忘记了文件名参数.

multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: namePDF, mimeType:"application/pdf")

感谢您的帮助.

这篇关于iOS Swift使用Alamofire上传PDF文件(多部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆