我使用以下代码将图像上传到PHP服务器(使用真实URL)
let imageData = UIImageJPEGRepresentation(image, 1)
if(imageData == nil ) { return }
let uploadScriptUrl = NSURL(string:"http://****/****/sendFile.php")
var request = NSMutableURLRequest(URL: uploadScriptUrl!)
request.HTTPMethod = "POST"
request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
request.setValue("multipart/form-data; boundary=*****", forHTTPHeaderField: "Content-Type")
request.setValue(Util.getPhonenum(), forHTTPHeaderField: "phonenumFrom")
request.setValue(self.theirPhonenum, forHTTPHeaderField: "phonenumTo")
request.setValue(Util.getPassword(), forHTTPHeaderField: "password")
request.setValue(obj.serialize(false), forHTTPHeaderField: "message")
request.setValue(".png", forHTTPHeaderField: "type")
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
var task = session.uploadTaskWithRequest(request, fromData: imageData)
task.resume()
如您所见,我已将内容类型设置为multipart / form-data
然而,这句话来自我的代表:
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void)
{
println("didReceiveResponse")
println(dataTask.response);
}
返回此(我删除了网址)
{URL:******。php} {status code:200,headers { 连接=“保持活力”; “内容长度”= 5; “Content-Type”=“text / html”; Date =“Wed,2015年8月19日20:59:12 GMT”; “Keep-Alive”=“timeout = 5,max = 100”; Server =“Apache / 2.4.7(Ubuntu)”; “X-Powered-By”=“PHP / 5.5.9-1ubuntu4.11”; }})
建议内容类型为text / html
为什么会这样?