下面我有此代码发送的图片和一些文字到我的服务器:NSURLSessionUploadTask不上传图片与参数
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: nil];
NSString *requestURL = @"http://www.website.com.br/receive.php?name=StackOverflow";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
[request setHTTPMethod:@"POST"];
UIImage *imagem = [UIImage imageNamed:@"Image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imagem, 1.0);
self.uploadTask = [self.session uploadTaskWithRequest:request fromData:imageData];
[self.uploadTask resume];
-(void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",newStr);
}
PHP
<?php
echo $_POST['name'];
?>
这段代码的问题是
012:该didReceiveData
方法不接收数据,传送至服务器,它只有当我在PHP文件把这段代码得到一个
然而它返回一个空数组,为什么发生这种情况?
解决
嗯,我解决我的问题,让我们去,在.h文件中,你需要实现这个协议和一个属性:
< NSURLSessionDelegate, NSURLSessionTaskDelegate>
@property (nonatomic) NSURLSessionUploadTask *uploadTask;
而在.m文件有一个IBAction类型的方法并且它连接到我们认为存在的特定按钮,我们只需要这样做:
- (IBAction)start:(id)sender {
if (self.uploadTask) {
NSLog(@"Wait for this process finish!");
return;
}
NSString *imagepath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"myImage.jpg"];
NSURL *outputFileURL = [NSURL fileURLWithPath:imagepath];
// Define the Paths
NSURL *icyURL = [NSURL URLWithString:@"http://www.website.com/upload.php"];
// Create the Request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:icyURL];
[request setHTTPMethod:@"POST"];
// Configure the NSURL Session
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.sometihng.upload"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
// Define the Upload task
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:outputFileURL];
// Run it!
[self.uploadTask resume];
}
并实现一些委托方法:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@"didSendBodyData: %lld, totalBytesSent: %lld, totalBytesExpectedToSend: %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error == nil) {
NSLog(@"Task: %@ upload complete", task);
} else {
NSLog(@"Task: %@ upload with error: %@", task, [error localizedDescription]);
}
}
而对于结果,你需要创建一个PHP文件与此代码:
<?php
$fp = fopen("myImage.jpg", "a");//If image come is .png put myImage.png, is the file come is .mp4 put myImage.mp4, if .pdf myImage.pdf, if .json myImage.json ...
$run = fwrite($fp, file_get_contents("php://input"));
fclose($fp);
?>
我的代码中有两个NSDatas,一个用于发送数据,另一个用于接收,我怎么把你的代码放在两个中? – LettersBa 2015-02-18 02:09:06
我的方法didReceiveData用于:取消归档数据和我收到一个错误崩溃:难以理解的存档(0×41,0x72,0x72,0x61,0x79的,是0xA,0×28,是0xA)” – LettersBa 2015-02-18 02:10:51
我解决我的问题,谢谢... – LettersBa 2015-02-18 14:19:51