iOS上傳影象到伺服器,以及伺服器PHP接收的幾種方法
阿新 • • 發佈:2018-11-19
iOS上傳影象到伺服器,以及伺服器PHP接收的幾種方法
1. 將圖片轉換為Base64編碼,POST上傳。PHP將Base64解碼為二進位制,再寫出檔案。缺點:不能上傳較大的圖片
// iOS(Swift)
func upload(image: UIImage, url: String) {
let imageData = UIImageJPEGRepresentation(image, 0.3) // 將圖片轉換成jpeg格式的NSData,壓縮到0.3
let imageStr = imageData?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) // 將圖片轉換為base64字串
let params: NSDictionary = ["file": imageStr!]
let manager = AFHTTPRequestOperationManager()
// 採用POST的方式上傳,因為POST對長度沒有限制
manager.POST(url, parameters: params, success: { (_: AFHTTPRequestOperation!, response: AnyObject!) in
// 成功
}) { (_: AFHTTPRequestOperation!, _: NSError!) in
// 失敗
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
<?php
header('Content-type: text/json; charset=UTF-8');
$base64 = $_POST["file"]; // 得到引數
$img = base64_decode($base64); // 將格式為base64的字串解碼
$path = "md5(uniqid(rand()))" .".jpg"; // 產生隨機唯一的名字作為檔名
file_put_contents($path, $img); // 將圖片儲存到相應位置
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.將圖片封裝在Http的請求報文中的請求體(body)中上傳。也是AFN上傳的原理
http://www.iashes.com/2016-03-1313.html
// 使用OC封裝
#import <UIKit/UIKit.h>
@interface RequestPostUploadHelper : NSObject
+ (NSMutableURLRequest *)uploadImage:(NSString*)url uploadImage:(UIImage *)uploadImage params:(NSMutableDictionary *)params;
@end
#import "RequestPostUploadHelper.h"
@implementation RequestPostUploadHelper
+ (NSMutableURLRequest *)uploadImage:(NSString*)url uploadImage:(UIImage *)uploadImage params:(NSMutableDictionary *)params {
[params setObject:uploadImage forKey:@"file"];
//分界線的識別符號
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//根據url初始化request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
//分界線 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//結束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
//要上傳的圖片
UIImage *image=[params objectForKey:@"file"];
//得到圖片的data
NSData* data = UIImagePNGRepresentation(image);
//http body的字串
NSMutableString *body=[[NSMutableString alloc]init];
//引數的集合的所有key的集合
NSArray *keys= [params allKeys];
//遍歷keys
for(int i = 0; i < [keys count]; i++)
{
//得到當前key
NSString *key = [keys objectAtIndex:i];
//如果key不是file,說明value是字元型別,比如name:Boris
if(![key isEqualToString:@"file"])
{
//新增分界線,換行
[body appendFormat:@"%@\r\n",MPboundary];
//新增欄位名稱,換2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//新增欄位的值
[body appendFormat:@"%@\r\n",[params objectForKey:key]];
}
}
////新增分界線,換行
[body appendFormat:@"%@\r\n",MPboundary];
//宣告file欄位,檔名為image.png
[body appendFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"image.png\"\r\n"];
//宣告上傳檔案的格式
[body appendFormat:@"Content-Type: image/png\r\n\r\n"];
//宣告結束符:--AaB03x--
NSString *end=[[NSString alloc] initWithFormat:@"\r\n%@",endMPboundary];
//宣告myRequestData,用來放入http body
NSMutableData *myRequestData = [NSMutableData data];
//將body字串轉化為UTF8格式的二進位制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//將image的data加入
[myRequestData appendData:data];
//加入結束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//設定HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//設定HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//設定Content-Length
[request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//設定http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
return request;
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
// 使用
// Swift
static func uploadPortrait(image: UIImage, url:String) {
// 使用
let request = RequestPostUploadHelper.uploadImage(url, uploadImage: image, params: [:])
// 非同步網路請求
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
if error != nil {
// 失敗
} else {
// 成功
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14