- (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

Content-Type:multipart/form-data;boundary=--ABCD  
2. post内容,各部分以--ABCD开头,结尾以--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 
 
注意:换行的地方也是要加\r\n的

post上传文件的更多相关文章

  1. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  2. 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader

    发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...

  3. asp.net mvc 上传文件

    转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...

  4. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  5. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. 前端之web上传文件的方式

    前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...

  8. Django session cookie 上传文件、详解

    session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...

  9. 4 django系列之HTML通过form标签来同时提交表单内容与上传文件

    preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...

  10. 1. Django系列之Django与ajax上传文件

    html代码如下: <div class="form-group"> <label for="exampleInputFile">附件上 ...

随机推荐

  1. Java Service Wrapper简介与使用

    在实际开发过程中很多模块需要独立运行,他们并不会以web形式发布,传统的做法是将其压缩为jar包独立运行,这种形式简单易行也比较利于维护,但是一旦服务器重启或出现异常时,程序往往无法自行修复或重启.解 ...

  2. vi 技巧和诀窍~转IBM

    复合搜索 1 #!/bin/ksh 2 # 3 echo "Starting" 4 file=${1} 5 6 echo ${file} 7 8 if [[ ${file} = 1 ...

  3. 阿里云服务器Linux CentOS安装配置(四)yum安装tomcat

    阿里云服务器Linux CentOS安装配置(四)yum安装tomcat 1.yum -y install tomcat  执行命令后,会帮你把jdk也安装好 2.tomcat安装目录:/var/li ...

  4. ajax原理和跨域解决方法

    ajax是异步的 JavaScript 和 XML.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 1--启动 ...

  5. 响应链和UIKit框架

    Event Delivery: The Responder Chain When you design your app, it’s likely that you want to respond t ...

  6. Cookie和Session的区别详解

    本文引用自:http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一 ...

  7. ElasticSearch 自定义排序处理

    使用function_score进行分组处理,利用分组函数script_score进行自定义分值处理, 注意:使用script功能需要在配置中打开脚本功能: script.inline: on   s ...

  8. Spark相关下载

    HBase: http://hbase.apache.org/ Hadoop hadoop.apache.org spark http://spark.apache.org/

  9. [原]简易android反编译教程

    1.apktool d -f <.apk> <output> 反编译出资源文件(及smali)2.使用winrar打开apk,提取出classed.dex,执行 d2j-dex ...

  10. 在Eclipse中使用Git提交到远程仓库