文件上传 ios开发

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIBarButtonItem *upButton = [[UIBarButtonItem alloc] initWithTitle:@"上传" style:UIBarButtonItemStylePlain target:self action:@selector(upLoadFile)];

self.navigationItem.rightBarButtonItem = upButton;

}

//上传文件的方法

-(void)upLoadFile{

//创建url

NSURL *url = [NSURL URLWithString:@"http://localhost:8080/Server/upload"];

//创建一个可变的POST请求

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

//设置请求方式 POST

[request setHTTPMethod:@"POST"];

//设置超时时间

[request setTimeoutInterval:20];

//设置要传的内容的类型

[request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];

//获取项目中的图片并转换成NSData类型

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];

//

//    NSData *data = [NSData dataWithContentsOfFile:path];

//获取我们从相册选取过的图片

UIImage *image = self.image_view.image;

//image转换nsdata

NSData *data = UIImagePNGRepresentation(image);

//设置请求体内容,也就要上传的图片

[request setHTTPBody:data];

//建立连接发送请求

[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

}

//点击按钮,从相册选取图片

- (IBAction)selectBtnClick:(id)sender {

//我们使用图片选择控制器

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

//设置要选择的来源类型(相册、相机等)

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//设置代理

picker.delegate = self;

//设置选择后的图片可以被编辑

picker.allowsEditing = YES;

//弹出选择界面

[self presentViewController:picker animated:YES completion:nil];

}

//选择图片的代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

self.image_view.image = image;

[picker dismissViewControllerAnimated:YES completion:nil];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

[picker dismissViewControllerAnimated:YES completion:nil];

}

@end

时间: 2024-08-02 09:48:56

文件上传 ios开发的相关文章

多文件上传 iOS

[1].[代码] [其他]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 NSURL* url = [NSURL URLWithString:@"xxx"];     ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:url];     request.uploadProgressDelegate = viewPost.progressView;     reque

文件上传的步骤

文件上传的步骤: 1.<input type="file" /> 2.首先要更改form的enctype="multipart/form-data" 表示向服务器传输的过程中以二进制的方式传输 默认传输类型: enctype="application/x-www-form-urlencoded" 3.相关jar包 下载地址 www.apache.org commons-fileupload-1.2.1.jar 核心组件包 common

文件上传(表单文件上传)

文件上传是开发一个网站最基本的一个需求功能 前台页面的设置: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerNa

IOS开发之文件上传

IOS开发之文件上传 在移动应用开发  文件形式上传是必不可少的,最近把IOS这块文件上传文件代码简单的整理一下,如果大家有需要安卓这边的代码,本人也可以分享给大家!QQ群:74432915  欢迎大家一起探讨 首先本demo采用网上开源框架 AFNetworking  源码:http://download.csdn.net/detail/wangliang198901/7809439 将整个框架导入IOS新建立的工程中 在FKAppDelegate.h声明 如下: #import <UIKit

iOS多线程与网络开发之小文件上传

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源代码传送:http://dwz.cn/Nret1 A.文件上传 思路: 发送文件数据给server 使用post请求 必须手动设置请求头: 内

iOS开发-AFNetworking参数和多文件同时上传【多文件上传】

1. 前言 在项目开发中,我们经常需要上传文件,例如:上传图片,上传各种文件,而有时也需要将参数和多个文件一起上传,不知道大家的项目中遇到了没有,我在最近的项目中,就需要这样的一个功能:同时上传参数.多张图片和音频: 2. 功能实现 在实现此功能之前,需要引入第三方AFNetworking,这是一个非常强大的网络开发工具,这里我就不多说了,下面是我用此工具,封装的一个同时上传参数和多个文件的方法: 2.1 方法名 /** 多文件上传,支持同时上传参数.多个图片.多个音频,[需要将每一个文件转化为

iOS开发-文件上传原理

文件上传 编写文件上传类UploadFile.h // // UploadFile.h // 02.Post上传 // // Created by wyh on 15-1-29. // Copyright (c) 2015年 itcast. All rights reserved. // #import <Foundation/Foundation.h> @interface UploadFile : NSObject - (void)uploadFileWithURL:(NSURL *)ur

ios开发网络学习十二:NSURLSession实现文件上传

#import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @"----WebKitFormBoundaryjv0UfA04ED44AhWx" #define KNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding] @interface ViewController ()&l

iOS开发之多文件上传

// //  ViewController.m //  B03-多文件上传 // //  Created by 0426iOS on 15/7/1. //  Copyright (c) 2015年 0426iOS. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {