post上传文件
- (BOOL)sendPhotoToTumblr:(NSString *)photo withCaption:(NSString *)caption;
{
//get image data from file
NSData *imageData = [NSData dataWithContentsOfFile:photo];
//stop on error
if (!imageData) return NO;
//Create dictionary of post arguments
NSArray *keys = [[NSArray alloc] initWithObjects:@"email",@"password",@"type",@"caption",nil];
NSArray *objects = [[NSArray alloc] initWithObjects:
[NSString stringWithFormat:@"%@",CFPreferencesCopyAppValue(CFSTR("TumblrEmail"), kCFPreferencesCurrentApplication)],
[NSString stringWithFormat:@"%@",CFPreferencesCopyAppValue(CFSTR("TumblrPassword"), kCFPreferencesCurrentApplication)],
@"photo", caption, nil];
NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
//create tumblr photo post
NSURLRequest *tumblrPost = [self createTumblrRequest:keysDict withData:imageData];
//send request, return YES if successful
tumblrConnection = [[NSURLConnection alloc] initWithRequest:tumblrPost delegate:self];
if (!tumblrConnection) {
NSLog(@"Failed to submit request");
return NO;
} else {
NSLog(@"Request submitted");
receivedData = [[NSMutableData data] retain];
return YES;
}
} -(NSURLRequest *)createTumblrRequest:(NSDictionary *)postKeys withData:(NSData *)data
{
//create the URL POST Request to tumblr
NSURL *tumblrURL = [NSURL URLWithString:@"http://www.tumblr.com/api/write"];
NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL];
[tumblrPost setHTTPMethod:@"POST"];
//Add the header info
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"];
//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//add key values from the NSDictionary object
NSEnumerator *keys = [postKeys keyEnumerator];
int i;
for (i = 0; i < [postKeys count]; i++) {
NSString *tempKey = [keys nextObject];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
} //add data field and file data
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:data]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//add the body to the post
[tumblrPost setHTTPBody:postBody]; return tumblrPost;
}
1. 设置HttpWebRequest的Content-Type,一定boundary,这里是--ABCD
2. post内容,各部分以--ABCD开头,结尾以--ABCD--结尾
Content-Disposition: form-data; name="title"
\r\n
Today
--ABCD
Content-Disposition: form-data; name="1.txt"; filename="C:\1.txt"
Content-Type: text/plain
\r\n
<这里是1.txt文件的内容>
--ABCD--\r\n
post上传文件的更多相关文章
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader
发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...
- asp.net mvc 上传文件
转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...
- app端上传文件至服务器后台,web端上传文件存储到服务器
1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- 前端之web上传文件的方式
前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...
- Django session cookie 上传文件、详解
session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...
- 4 django系列之HTML通过form标签来同时提交表单内容与上传文件
preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...
- 1. Django系列之Django与ajax上传文件
html代码如下: <div class="form-group"> <label for="exampleInputFile">附件上 ...
随机推荐
- ubuntu实现ramdisk
1. linux内核提供了16个ramdisk供使用者使用,只需格式化,并挂在便可以使用.查看 ls /dev/ram* 2. 修改配置文件: sudo gedit /etc/default/grub ...
- windows 系统时钟
偶然发现了一个函数用以查询操作系统的时钟间隔: BOOL WINAPI GetSystemTimeAdjustment( _Out_ PDWORD lpTimeAdjustment, _Out_ PD ...
- 设置一个顺手的Xcode
授权转载,作者:吴白(微博) 手指在键盘上飞速跳跃,终端上的代码也随着飞舞,是的这确实很酷.优秀的程序员总是这么一群人,他们不拘于现状,不固步自封,他们喜欢新奇的事,他们把自己发挥到极致. 指法攻略 ...
- merge 本地 master 分支代码提示 “Already up-to-date”
在使用 git 的过程中由于误操作,导致从本地 master 分支 merge 代码到当前分支失败,虽然当前分支和 master 分支代码不同步,但是仍然提示 Already up-to-date. ...
- 另一种在WINFORM中使用XNA的方法
之前在写化学分子模型制作程序的时候,使用一种方法,将WINFORM控件嵌入到XNA窗体中,从而实现了即使用WINFORM窗体控件又使用XNA.最近在写另一个物理运动学课件制作程序,同样使用XNA,但从 ...
- django--主要部分
django URL URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个U ...
- Ehcache Demo
转自: https://my.oschina.net/zb0423/blog/60957http://www.cnblogs.com/fsjin/articles/3521261.html Ehcac ...
- static 使用要注意的地方
protected static string headimg = string.Empty; 这里用到 static ,下面如果这样写 object himg = DBUtility.DbH ...
- 一、oracle数据库成功安装步骤 (11gR2)
下载安装包 从Oracle官方网站下载数据库软件安装包:http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloa ...
- Linux CentOS 中安装 MySql
CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1. 下载mysql的repo源 $ wget http://repo.mysql.com ...