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