iOS客戶端上傳圖片&&Thinkphp Upload();方法

多圖上傳
NSString *mnPath = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
    NSString *cyPath = [[NSBundle mainBundle] pathForResource:@"草原.jpg" ofType:nil];
    UIImage *mnImage = [[UIImage alloc] initWithContentsOfFile:mnPath];
    UIImage *cyImage = [[UIImage alloc] initWithContentsOfFile:cyPath];
    NSData *mnData = UIImageJPEGRepresentation(mnImage, 0.7f);
    NSData *cyData = UIImageJPEGRepresentation(cyImage, 0.7f);
    NSArray *dataArray = @[mnData,cyData];
    
    //發(fā)起網(wǎng)絡(luò)請(qǐng)求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        // 上傳多張圖片
        for(NSInteger i = 0; i < dataArray.count; i++)
        {
            // 取出單張圖片二進(jìn)制數(shù)據(jù)
            NSData * imageData = dataArray[i];
            
            // 圖片數(shù)據(jù)對(duì)應(yīng)的key值,沒(méi)有這個(gè)key 服務(wù)器$_FILES 獲取不到數(shù)據(jù)流
            NSString * Name = [NSString stringWithFormat:@"%ld", i+1];
            
            // 圖片名
            NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
            [formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

單圖上傳

    NSString *path = [[NSBundle mainBundle] pathForResource:@"WE.jpg" ofType:nil];
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:path];
    NSData *data = UIImageJPEGRepresentation(savedImage, 0.7f);
    
    // 發(fā)起網(wǎng)絡(luò)請(qǐng)求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [manager POST:@"http://thinkphp3.2.cc:8888/Api/v1/Index/upload_goods" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        // name :圖片數(shù)據(jù)對(duì)應(yīng)的key值,沒(méi)有這個(gè)key 服務(wù)器$_FILES 獲取不到數(shù)據(jù)流
        
        [formData appendPartWithFileData:data name:@"file" fileName:@"WE.jpg" mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

服務(wù)端

     /**
     * 多圖/單圖上傳
     */

    public function upload_goods(){

        $upload = new \Think\Upload();// 實(shí)例化上傳類
        $upload->maxSize = 4000000 ;// 設(shè)置附件上傳大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設(shè)置附件上傳類型
        $upload->rootPath = './uploads/goods_img/'; // 設(shè)置附件上傳根目錄
        $upload->savePath = ''; // 設(shè)置附件上傳(子)目錄
        $upload->autoSub = true;
        // 上傳文件
        $info = $upload->upload($_FILES);
        if(!$info) {
            $this->ajaxReturn(apiData($upload->getError(), true));
        }else{
            $this->ajaxReturn(apiData($info, true));
        }
    }

返回?cái)?shù)據(jù)

backData.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容