Swift file upload and form submission

Source: Internet
Author: User

var cardata:nsmutabledictionarynsmutabledictionary();   
var request:nsmutableurlrequestnsmutableurlrequest();   
Request. URLnsurl"YOUR url ADDR");    
Request. httpmethod"POST";  
Request. timeoutinterval;  
var body:nsmutabledatansmutabledata();   
Set Form separators
var boundary:nsstring"----------------------1465789351321346";   
varnsstring"multipart/form-data;boundary=%@", boundary);  
Request. addvalue"Content-type");  
Write Info content  
var keys:nsarray = cardata.  AllKeys;    
For in keys{ 
Body. appendData(nsstring"--%@\r\n", boundary).  Datausingencoding(nsutf8stringencoding)!);        
Body. appendData(nsstring"content-disposition:form-data;name=\"%@\ "\r\n\r\n"nsstring).  Datausingencoding(nsutf8stringencoding)!);         
If there is a Chinese UTF8 encoding body. appendData("\ (cardata.  ObjectforkeyString)\ r \ n ".  Datausingencoding(nsutf8stringencoding)!)           
}
Write picture content
varnshomedirectory() + (cardata.  Valueforkey("imagesrc"String);      
println(Imgpath)
Body. appendData(nsstring"--%@\r\n", boundary).  Datausingencoding(nsutf8stringencoding)!);        
Body. appendData(nsstring"content-disposition:form-data;name=\"%@\ "; filename=\"\ (imgpath)\ "\ r\n "" UserFile ").  Datausingencoding(nsutf8stringencoding)!);           
var imageData:nsdatauiimagejpegrepresentation(UIImage1);     
Body. appendData("content-type:image/jpeg\r\n\r\n").  Datausingencoding(nsutf8stringencoding)!)       
Body. appendData(imageData); 
Body. appendData("\ r \ n").  Datausingencoding(nsutf8stringencoding)!)       
Write Trailer
Body. appendData(nsstring"--%@--\r\n", boundary).  Datausingencoding(nsutf8stringencoding)!);        
Request. httpbody = body; 
var urlresponse:nshttpurlresponsenil;   
var error:nserrornserror();//third party determines whether the network is connected   
Ijreachability.  Isconnectedtonetwork() {  
Nsurlconnection.  Sendasynchronousrequestnsoperationqueue    in
Ifnil) { 
var json:nsdictionarynsjsonserialization.  Jsonobjectwithdatansjsonreadingoptionsnilnsdictionary;        
Dispatch_async(dispatch_get_main_queue(), {  
Loading. modembprogresshudmode.  Text;    
If(json["Success"nil) {//mbprogresshud hint plugin   
Loading. modembprogresshudmode.  CustomView;    
Loading. customviewuiimageviewUIImage"37x-checkmark"))    
Loading. labelText" upload success ";    
}if(json["failed"nil) {    
Loading. labelText = json["Failed"String;    
}
Loading. Hide(true1);    
println(JSON);
})
}Else{ 
Dispatch_async(dispatch_get_main_queue(), {  
Loading. modembprogresshudmode.  Text;    
Loading. LabelText = error.  Localizeddescription;   
Loading. Hide(true1);    
println(error.  Localizeddescription);  
})
}
}
}Else{ 
Loading. modembprogresshudmode.  CustomView;    
Loading. customviewuiimageviewUIImage"Wrongmark"))    
Loading. labelTextnonetworkmsg;  
Loading. Hide(true1);    
}

:


Reproduced this site article please indicate the source: Love code http://blog.vlian888.com/?p=105

Additional Articles Link: 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

Complete use of Uiimagepickercontroller to select images and upload examples (image upload example with Swift and PHP) http://swiftdeveloperblog.com/ image-upload-example/

Swift file upload and form submission

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.