螢幕快照 2016-03-02 上午1.57.59  

 

    /**

     * Content-Type: multipart/form-data

     */

    func multiPartFormData(parameter: TParameter?, callback: (model: TResult?) -> Void)

    {

   

        #ifDEBUG

            print("url: \(nsUrl.description)")

        #endif

        

        let boundary = "AaB03x"

        let request = NSMutableURLRequest(URL: nsUrl, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60)

        

        request.HTTPMethod = "POST"

        

        // Http Header

        request.addValue(WebConfig.shareConfig.userAgent, forHTTPHeaderField: "User-Agent")

        request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

        

        let MPboundary = "--\(boundary)"

        let endMPboundary = "\(MPboundary)--"

        

        // Http Body

        ifl et parameter = parameter

        {

            let bodyData = NSMutableData()

            var bodyString: String = ""

            

            let mirrorForModel = Mirror(reflecting: parameter)

            

            // 取得所有 Int, String 屬性

            for (label, value) in mirrorForModel.children

            {

                guardlet value = Utilities.unwrap(value) else

                {

                    continue

                }

                

                if !(value isNSData)

                {

                    // with other params

                    bodyString += "\(MPboundary)\r\n"

                    bodyString += "Content-Disposition: form-data; name=\"\(label!)\"\r\n\r\n"

                    bodyString += "\(value)\r\n"

                }

                    // 取得所有 NSData 屬性

                elseif value isNSData

                {

                    // set upload image, name is the key of image

                    bodyData.appendData("\(MPboundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

                    bodyData.appendData("Content-Disposition: form-data; name=\"\(label!)\"; filename=\"\(label!).jpg\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

                    bodyData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

                    

                    bodyData.appendData(value as! NSData)

                    

                    bodyData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

                }

 

            }

            

            let end = "\(endMPboundary)"

            

            // 字串加入NSData

            let body = NSMutableData()

            

            body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding)!)

            body.appendData(bodyData)

            body.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!)

            

            request.addValue("\(body.length)", forHTTPHeaderField: "Content-Length")

            request.HTTPBody = body

            

            #ifDEBUG

            print("HttpBody(1/2):\(bodyString ?? "")\r\n")

            print("BodySize: \(body.length)")

            print("HttpBody(完整):\(String(data: body, encoding: NSUTF8StringEncoding))\r\n")

            #endif

        }

        

        // Async

        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(request) { (data, response, error) -> Voidin

            

            let result = self.parser.parse(self.nsUrl, data: data, response: response, error: error)

            

            // UI Thread

            dispatch_async(dispatch_get_main_queue(), { () -> Voidin

                

                callback(model: result)

            })

            

        }

        

        task.resume()

    }

 

螢幕快照 2016-02-27 下午10.03.39  

(Postman使用Raw Value送Multipart/form-data的方式)

 

 

參考資料:

http://jjnnykimo.pixnet.net/blog/post/21585315-http%E8%AB%8B%E6%B1%82%E9%A0%AD%E6%A6%82%E8%BF%B0

http://stackoverflow.com/questions/31406022/how-is-an-http-multipart-content-length-header-value-calculated

http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean

 

http://stackoverflow.com/questions/29623187/upload-image-with-multipart-form-data-ios-in-swift

http://stackoverflow.com/questions/26162616/upload-image-with-parameters-in-swift

http://stackoverflow.com/questions/10002821/how-do-i-post-an-array-with-multipart-form-data-encoding

 

arrow
arrow
    全站熱搜

    小賢 發表在 痞客邦 留言(0) 人氣()