我當前將圖像上傳到服務器的背景中(名稱在服務器上更改爲更好)。將NSURLConnection更改爲NSURLSession以上傳圖像
buildURL = [buildURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0); //change Image to NSData
if (imageData != nil)
{
NSString *filenames = [NSString stringWithFormat:@"imagename"]; //set name here
NSLog(@"%@", filenames);
NSString *urlString = buildURL;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Returned From Image Upload : %@",returnString);
}];
}
這工作正常,我可以看到圖像在後臺完成沒有問題。事情是我想改變這個使用NSURLSession和委託didSendBodyData,以便我可以監控上傳。
我發現了一些關於下載的信息,但沒有一個關於上傳。我曾嘗試與要求,但完成塊做到這永遠不會發生。我已經使用uploadTaskWithStreamedRequest也試過,但我不能讓委託的情況發生......
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURLSessionDataTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"Image Uploaded ------------");
}];
[uploadTask resume];
我也發現了這個StackOverflow 19985353這似乎突出顯示一些問題,但我真的可以使用一些示例代碼。如果有幫助,圖像將從imagePicker中挑選出來。
我也在這裏嘗試了這兩個答案StackOverflow 19099448但他們都沒有工作,再次從未執行塊。有沒有什麼基礎我正在做錯NSURLSession,一個框架也許!我也注意到我不知道我嘗試過的HTTP身體,但沒有運氣。
這可以幫助你:[教程](http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&ved=0CDcQFjAC&url=http%3A%2F %2Fwww.raywenderlich.com%2F51127%2Fnsurlsession-tutorial&ei = _IXzUqq3MJOMyAHhx4DwDA&usg = AFQjCNG8YRDnx7buLt1VGpFYqkoE91cXYg&bvm = bv.60799247,d.aWc) – 68cherries
@ 67cherries我經歷過這個但仍然沒有快樂。我嘗試了所有的例子,但我簡單的不能讓委託和/或塊運行。有沒有錯誤... –
你使用多線程? – 68cherries