안녕 지금이미지 나 서버에 데이터 형식 (이미지 데이터/비디오 데이터)으로 multipart 할 스위프트
var request = NSMutableURLRequest(URL: NSURL(string: "http://mypath.com"))
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var params2 :NSDictionary = ["email":"[email protected]"]
var params :NSDictionary = ["function":"forgotPassword", "parameters":params2]
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
let decodedJson = NSJSONSerialization.JSONObjectWithData(data, options: nil, error:nil) as NSDictionary
println("Body: \(strData)")
//Here I am getting response
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console
if(err != nil) {
println(err!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: '\(jsonStr)'")
}
else {
// The JSONObjectWithData constructor didn't return an error. But, we should still
// check and make sure that json has a value using optional binding.
if let parseJSON = json {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
var success = parseJSON["success"] as? Int
println("Succes: \(success)")
}
else {
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: \(jsonStr)")
}
}
})
task.resume()
아래로 빠른에 NSURLSession를 사용하여 서버에 JSON 데이터를 게시하고의 서버에 게시 (또는 비디오) 키 값 "영상"대물 CI에서 USER_ID = 15 about_video_thumb = "myImagejpg"
같은 다른 파라미터들과 함께 아래
-(void)upLoadThumb{
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://mypath.com/uplaodpic"]];
[request setHTTPMethod:@"POST"];
//Setting content type - multipart/form-data
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// This is one field user_id
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", @"15"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"about_video_thumb\"; filename=\"%@\"\r\n", @"myImage.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
//Appending NSDATA
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:appDelegate.userVdoImageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [NSMutableData data];
}
}
같은 이미지 uplaoding 오전 어떻게 신속하게 NSURLSession 또는 NSURLConnection을 사용하여이 작업을 수행 할 수 있습니까? 미리 감사드립니다.
나는 그것을 얻지 못한다. 서버에서 이미지를 다운로드 하시겠습니까? – mustafa
아니요 서버로 이미지를 데이터로 게시하고 싶습니다. –