swift文件上传及表单提交

var carData:NSMutableDictionary = NSMutableDictionary();
var request:NSMutableURLRequest = NSMutableURLRequest();
        request.URL = NSURL(string: "YOUR URL ADDR");
        request.HTTPMethod = "POST";
        request.timeoutInterval = 10;
        var body:NSMutableData = NSMutableData();
        
        //设置表单分隔符
        var boundary:NSString = "----------------------1465789351321346";
        var contentType = NSString(format: "multipart/form-data;boundary=%@", boundary);
        request.addValue(contentType, forHTTPHeaderField: "Content-Type");
        
        //写入Info内容
        var keys:NSArray = carData.allKeys;
        for key in keys{
            body.appendData(NSString(format: "--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!);
            body.appendData(NSString(format: "Content-Disposition:form-data;name=\"%@\"\r\n\r\n", key as NSString).dataUsingEncoding(NSUTF8StringEncoding)!);
//如果有中文进行UTF8编码
            body.appendData("\(carData.objectForKey(key) as String)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        }
        //写入图片内容
        var ImgPath = NSHomeDirectory()+(carData.valueForKey("imageSrc") as String);
        println(ImgPath)
        body.appendData(NSString(format: "--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!);
        body.appendData(NSString(format: "Content-Disposition:form-data;name=\"%@\";filename=\"\(ImgPath)\"\r\n", "userfile").dataUsingEncoding(NSUTF8StringEncoding)!);
        var imageData:NSData = UIImageJPEGRepresentation(UIImage(contentsOfFile: ImgPath), 1);
        body.appendData("Content-Type:image/jpeg\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        body.appendData(imageData);
        body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
        
        
        
        
        //写入尾部
        body.appendData(NSString(format: "--%@--\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!);
        request.HTTPBody = body;
        
        var urlResponse:NSHTTPURLResponse? = nil;
        var error:NSError? = NSError();

//第三方判断网络是否连接
        if IJReachability.isConnectedToNetwork() {
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response, data, error) -> Void in
                if(error == nil){
                    var json:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary;
                    dispatch_async(dispatch_get_main_queue(), {
                        loading.mode = MBProgressHUDMode.Text;
                        if(json["success"] != nil){

//MBProgressHUD提示插件
                            loading.mode = MBProgressHUDMode.CustomView;
                            loading.customView = UIImageView(image: UIImage(named: "37x-Checkmark"))
                            loading.labelText = "上传成功";
                        }else if(json["failed"] != nil){
                            loading.labelText = json["failed"] as String;
                        }
                        loading.hide(true, afterDelay: 1);
                       // println(json);
                    })
                }else{
                    dispatch_async(dispatch_get_main_queue(), {
                        loading.mode = MBProgressHUDMode.Text;
                        loading.labelText = error.localizedDescription;
                        loading.hide(true, afterDelay: 1);
                        println(error.localizedDescription);
                    })
                }
            }
        }else{
            loading.mode = MBProgressHUDMode.CustomView;
            loading.customView = UIImageView(image: UIImage(named: "Wrongmark"))
            loading.labelText = noNetworkMsg;
            loading.hide(true, afterDelay: 1);
        }

效果图:

 

iOS Simulator Screen Shot 2015年5月11日 下午4.48.58

F0B8A55E-5816-498F-92FB-AA184EB6FED0

转载本站文章请注明出处:爱代码 http://blog.vlian888.com/?p=105

 

 

另外的一些文章链接:http://www.cocoachina.com/bbs/read.php?tid-284255.html

http://www.360doc.com/content/15/0612/17/11417867_477671614.shtml

http://www.cnblogs.com/zhwl/archive/2013/01/04/2843788.html

 

完整的利用UIImagePickerController 选取图片并且上传的例子(Image upload example with Swift and PHP)  http://swiftdeveloperblog.com/image-upload-example/

posted @ 2016-01-10 22:43  zengsiyu  阅读(1441)  评论(0编辑  收藏  举报