• 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: "--%@
    ", boundary).dataUsingEncoding(NSUTF8StringEncoding)!);
                body.appendData(NSString(format: "Content-Disposition:form-data;name="%@"
    
    ", key as NSString).dataUsingEncoding(NSUTF8StringEncoding)!);
    //如果有中文进行UTF8编码
                body.appendData("(carData.objectForKey(key) as String)
    ".dataUsingEncoding(NSUTF8StringEncoding)!)
            }
            //写入图片内容
            var ImgPath = NSHomeDirectory()+(carData.valueForKey("imageSrc") as String);
            println(ImgPath)
            body.appendData(NSString(format: "--%@
    ", boundary).dataUsingEncoding(NSUTF8StringEncoding)!);
            body.appendData(NSString(format: "Content-Disposition:form-data;name="%@";filename="(ImgPath)"
    ", "userfile").dataUsingEncoding(NSUTF8StringEncoding)!);
            var imageData:NSData = UIImageJPEGRepresentation(UIImage(contentsOfFile: ImgPath), 1);
            body.appendData("Content-Type:image/jpeg
    
    ".dataUsingEncoding(NSUTF8StringEncoding)!)
            body.appendData(imageData);
            body.appendData("
    ".dataUsingEncoding(NSUTF8StringEncoding)!)
            
            
            
            
            //写入尾部
            body.appendData(NSString(format: "--%@--
    ", 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/

  • 相关阅读:
    解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))
    解读Unity中的CG编写Shader系列八(多光源漫反射)
    Unity3d《Shader篇》漫反射
    解读Unity中的CG编写Shader系列八(镜面反射)
    渲染物体到一张UITexture上
    解读Unity中的CG编写Shader系列六(漫反射)
    解读Unity中的CG编写Shader系列五(理论知识)
    解读Unity中的CG编写Shader系列四(unity中的圆角矩形shader)
    解读Unity中的CG编写Shader系列三
    解读Unity中的CG编写Shader系列二
  • 原文地址:https://www.cnblogs.com/zengsiyu/p/5119774.html
Copyright © 2020-2023  润新知