直接上代码
let uploadurl:String="http://192.168.1.158/wsdata/default.aspx?class=i"//设置服务器接收地址
let request=NSMutableURLRequest(URL:NSURL(string:uploadurl)!)
request.HTTPMethod="POST"//设置请求方式
let boundary:String = "-------------------21212222222222222222222"
let contentType:String = "multipart/form-data;boundary="+boundary
request.addValue(contentType, forHTTPHeaderField:"Content-Type")
let body = NSMutableData()
//添加一个普通参数
body.appendData(NSString(format:"\r\n--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition:form-data;name=\"qdzf\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Type:text/plain;charset=utf-8\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"qiandaozifu").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"\r\n--\(boundary)").dataUsingEncoding(NSUTF8StringEncoding)!)
if image_1.image != nil {
//限制图片大小
let c = UIImagePNGRepresentation(image_1.image!)
let a:CGFloat = 100000.0
let b:CGFloat = CGFloat((c?.length)!)
let d = a/b
//图片转成data
let data1 = UIImageJPEGRepresentation(image_1.image!, d)
body.appendData(NSString(format:"\r\n--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition:form-data;name=\"userfile\";filename=\"dd1.jpg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Type:application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(data1!)
body.appendData(NSString(format:"\r\n--\(boundary)").dataUsingEncoding(NSUTF8StringEncoding)!)
}
if image_2.image != nil {
let data2 = UIImagePNGRepresentation(image_2.image!)
body.appendData(NSString(format:"\r\n--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition:form-data;name=\"userfile\";filename=\"dd2.jpg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Type:application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(data2!)
body.appendData(NSString(format:"\r\n--\(boundary)").dataUsingEncoding(NSUTF8StringEncoding)!)
}
if image_3.image != nil {
let data3 = UIImageJPEGRepresentation(image_3.image!, 1.0)
body.appendData(NSString(format:"\r\n--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Disposition:form-data;name=\"userfile\";filename=\"dd3.jpg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(NSString(format:"Content-Type:application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(data3!)
body.appendData(NSString(format:"\r\n--\(boundary)").dataUsingEncoding(NSUTF8StringEncoding)!)
}
request.HTTPBody=body
let que = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: que) { (response, data, error) -> Void in
if (error != nil){
print(error)
}
else
{
let tr:String=NSString(data: data!, encoding: NSUTF8StringEncoding)! as String
print(tr)
}
}
有不清楚的可以给我留言,看到我会回复的。