在 iOS 9 问题上使用 Alamofire 上传带有 URL 参数的图像

Posted

技术标签:

【中文标题】在 iOS 9 问题上使用 Alamofire 上传带有 URL 参数的图像【英文标题】:Upload image with URL parameters using Alamofire on iOS 9 issue 【发布时间】:2015-10-01 13:01:39 【问题描述】:

我有一个用 Swift 2.0 编写的 ios 应用程序,它可以将图像上传到 Microsoft Azure Blob 存储。我使用 Alamofire 来处理请求。

在装有 iOS 8 的设备上一切正常,但在 iOS 9 上上传时出错:

    let AlamofireManager = Alamofire.Manager.sharedInstance
    AlamofireManager.session.configuration.HTTPAdditionalHeaders = [
        "Content-Type": "image/jpg",
        "x-ms-blob-type": "BlockBlob"
    ]
    AlamofireManager.upload(.PUT, uploadURL, data: imageData)
        .responseJSON  (request, response, result) in
            print("Request=\(request)")
            print("response=\(response)")
            print("result=\(result)")
            if (response) != nil 
                let statusString = String(stringInterpolationSegment: response!.statusCode)
                let statusCode = self.getStatusCodeFromStatusString(statusString)
                if (statusCode == 201) 
                    // MY SUCCESS CODE IS HERE
                
                else 
                    // STATUSCODE != 201
                
            
            else 
                // OTHER ERROR
            
       

上传的示例 URL (uploadURL) 可能是:

https://mystorage.blob.core.windows.net:443/thumbs/7346e38a-eb54-48ea-b0fe-89357100dd18.jpg?sv=2013-08-15&sr=b&sig=GYwHvnUc52GsajFJCAu1v4W5qG0wSBpaXvxncD%2FAt34%3D&st=2015-10-01T11%3A25%3A57Z&se=2015-10-01T11%3A40%3A57Z&sp=w

对于 Azure,重要的是: a) HTTP 动词是PUT b) Url 参数包含在 Url 中,而不是作为多部分数据(这是上传的访问令牌)。

也许 Alamofire 在创建带有参数和 PUT 的 URL 时遇到问题?

此解决方案不起作用,因为它基于分段数据上传:Upload image with parameters in Swift

因为想到网址参数的问题,我尝试了以下:

    let AlamofireManager = Alamofire.Manager.sharedInstance
    AlamofireManager.session.configuration.HTTPAdditionalHeaders = [
        "Content-Type": "image/jpg",
        "x-ms-blob-type": "BlockBlob"
    ]
    var URLRequest: NSMutableURLRequest 
        let URL = NSURL(string: uploadURL)!
        var mutableRequest = NSMutableURLRequest(URL: URL)
        mutableRequest.HTTPMethod = Alamofire.Method.PUT.rawValue
        return mutableRequest
    
    AlamofireManager.upload(URLRequest, data: imageData)
:
: rest is same like above
:

但这也不起作用。在这两种情况下,我总是从服务器收到 400 错误(response.statusCode)。 result 包含 Failure 和信息 NSCocoaErrorDomain - code 3840 以及描述 Invalid value around character 3

但是在 iOS 8 上,这个解决方案就像一个魅力:(

任何想法,问题是什么以及如何解决?

编辑:或者端口可能是问题所在?

【问题讨论】:

【参考方案1】:

我自己搞定的。 Alamofire 似乎没有按照 AlamofireManager 的预期发送 HTTP-Headers。我手动添加了标题:

    var URLRequest: NSMutableURLRequest 
        let URL = NSURL(string: uploadURL)!
        var mutableRequest = NSMutableURLRequest(URL: URL)
        mutableRequest.HTTPMethod = Alamofire.Method.PUT.rawValue
        mutableRequest.setValue("image/jpg", forHTTPHeaderField: "Content-Type")
        mutableRequest.setValue("BlockBlob", forHTTPHeaderField: "x-ms-blob-type")
        return mutableRequest
    

    AlamofireManager.upload(URLRequest, data: imageData)
    :
    :

【讨论】:

Alamofire 完全按照预期发送标题。您需要在创建 URL 会话之前创建自己的会话配置并添加标头,否则 iOS 9 将不会附加标头。

以上是关于在 iOS 9 问题上使用 Alamofire 上传带有 URL 参数的图像的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 15 中未分配 Alamofire 4.9.1 sessionManager.delegate.sessionDidReceiveChallenge

尝试使用 Alamofire 检查 iOS 设备上的互联网连接

Swift 2 Json Alamofire 错误“..expects 1 个参数,但指定了 3 个”iOS 9 Xcode 7

iOS 9 中的 Alamofire 不工作

Alamofire 上传仅在 iphone 6s plus 上崩溃

Swift - 使用 alamofire 发布请求