duanqujing3863 2013-11-18 06:42
浏览 121

从ios上传文件内容

I have to upload file content using POST method. Problem is the server is on PHP and its takes the file content in the format below

$node->body = array('und' => array( 0 => array(
'value' => 'file content',
'format' => 'filtered_html')));

else other parameters are uploaded correctly except body. Please suggest how to send body parameters in the script so that it accept it correctly.

  • 写回答

1条回答 默认 最新

  • dql7588 2013-11-18 06:47
    关注

    Following code is describe simple example with POST method.(How can pass data by POST method)

    You can use the following code snippet, as described in this article:

    Here, I simple describe how can use of POST method.

    1. Set post string with actual username and password.

    NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
    

    2. Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    

    You need to send the actual length of your data. Calculate the length of the post string.

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    

    3. Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string. Create URLRequest object and initialize it.

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    

    Set the Url for which your going to send the data to that request.

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]];
    

    Now, set HTTP method (POST or GET). Write this lines as it is in your code.

    [request setHTTPMethod:@"POST"];
    

    Set HTTP header field with length of the post data.

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    

    Also set the Encoded value for HTTP header Field.

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
    

    Set the HTTPBody of the urlrequest with postData.

    [request setHTTPBody:postData];
    

    4. Now, create URLConnection object. Initialize it with the URLRequest.

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    

    It returns the initialized url connection and begins to load the data for the url request. You can check that whether you URL connection is done properly or not using just if/else statement as below.

    if(conn)
    {
    NSLog(@”Connection Successful”)
    }
    else
    {
    NSLog(@”Connection could not be made”);
    }
    

    5. To receive the data from the HTTP request , you can use the delegate methods provided by the URLConnection Class Reference. Delegate methods are as below.

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
    

    Above method is used to receive the data which we get using post method.

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    

    This method , you can use to receive the error report in case of connection is not made to server.

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    

    The above method is used to process the data after connection has made successfully.

    Also Refer This and This documentation for POST method.

    And here is best example with source code of HTTPPost Method.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?