在網上一搜索AFN上傳圖片,一大堆:app
`NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"image"] = newImageData; params[@"fileName"] = fileName; params[@"oldFile"] = self.users.photo; params[@"userid"] = @(self.users.ID);post
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; [SVProgressHUD show]; [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { // 在這個block中設置須要上傳的文件 [formData appendPartWithFileData:newImageData name:@"image" fileName:@"test.png" mimeType:@"image/png"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if ([[responseObject objectForKey:@"code"] intValue] != 200) { [SVProgressHUD showErrorWithStatus:@"上傳頭像失敗"]; } else { [SVProgressHUD showSuccessWithStatus:@"上傳頭像成功"]; !self.selectPhotpCompleteBlock ? : self.selectPhotpCompleteBlock(newImageData); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { TbLog(@"%@",[error localizedDescription]); [SVProgressHUD showErrorWithStatus:[error localizedDescription]]; }];· 其中·[formData appendPartWithFileData:newImageData name:@"image" fileName:@"test.png" mimeType:@"image/png"]; 這一句很是重要,拼接上傳圖片的post請求的請求頭信息,fileName,文件名稱,隨便寫,mineType:標示上傳的圖片類型,規定的 其中name很重要,很重要,是後臺上傳的key,這個是後臺規定的,要跟後臺協商,不一樣的後臺,可能寫圖片上傳的key,就不同,這個要問後臺了,我就是沒有跟後臺協商,一直搞不定上傳圖片,最後搞了很久才發現了,這個問題,浪費了很多時間`