下面是片斷上傳一個圖片file.I只想用NSURLSessionUploadTask,因爲它給了後臺上傳功能,我想在我的應用程序使用。NSURLSessionUploadTask上傳文件失敗
另外我想POST參數以及文件上傳。
而且我不是在服務器端好code.Can請人指導我什麼我做錯了什麼。
上傳代碼
NSString* filePath = [[NSBundle mainBundle]
pathForResource:@"sample" ofType:@"jpg"];
uint64_t bytesTotalForThisFile = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileSize];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uploadPath];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%llu", bytesTotalForThisFile] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request setTimeoutInterval:30];
NSString* filePath = [[NSBundle mainBundle]
pathForResource:@"sample" ofType:@"jpg"];
NSURLSessionUploadTask *taskUpload= [self.uploadSession uploadTaskWithRequest:request fromFile:[[NSURL alloc] initFileURLWithPath:filePath]];
PHP代碼
<?php
$objFile = & $_FILES["file"];
$strPath = basename($objFile["name"]);
if(move_uploaded_file($objFile["tmp_name"], $strPath)) {
print "The file " . $strPath . " has been uploaded.";
} else {
print "There was an error uploading the file, please try again!";
}
從服務器端我得到了$ _FILES
我使用的背景會話不會違約,而且完成處理程序沒有在後臺session.Also NSData的允許作爲圖像是不允許的圖像。 – Mukesh