使用两个参数iOS将文件发送到服务器iOS [英] Sending file to server iOS with two parameters iOS

查看:127
本文介绍了使用两个参数iOS将文件发送到服务器iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用参数filepath作为参数发送文件到服务器路径,并将数据作为另一个参数文件。我如何做到这一点。在下面我正在附加数据的文件路径。但我想这是错误的,请帮助

  NSURL * nsurl = [NSURL URLWithString:_urlString]; 
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setURL:nsurl];
[request setHTTPMethod:@POST];


NSString * boundary = @--------------------------- 14737809831466499882746641449;
NSString * contentType = [NSString stringWithFormat:@multipart / form-data; boundary =%@,boundary];
[request addValue:contentType forHTTPHeaderField:@Content-Type];

NSMutableData * body = [NSMutableData data];
NSData * data = UIImageJPEGRepresentation([UIImage imageNamed:@Model.png],0.0);
NSString * string = [NSString stringWithFormat:@filepath =%@,_ filePath];

NSData * pathData = [string dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:pathData];
$ b $ //图像
[body appendData:[[NSString stringWithFormat:@Content-Disposition:form-data; name = \image\; filename = \% @ \\r\\\
,@newFile.png] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@Content-Type:application / octet-stream \r\\\
\r\\\
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//将帖子正文设置为reqeust
[request setHTTPBody:body];
NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:nil];
NSLog(@%@,dict);

谢谢

解决方案 div>

你不能只在开始时执行 [body appendData:pathData]; 。您需要添加适当的边界和内容信息。所以你应该有许多行:


  1. 添加边界

  2. 添加内容信息
  3. 添加内容数据
  4. 添加边界


  5. <对于每一个附加的数据要添加2个重复。



    检查 spec ,了解每种数据类型需要添加的适当内容类型和配置信息。在底部)。


    I want to send file to server path with a paramater "filepath" as one parameter, and file data as another paramater. How do I do it.Here in the following I am appending filepath with data. but I guess it is wrong please help

    NSURL *nsurl =[NSURL URLWithString:_urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setURL:nsurl];
    [request setHTTPMethod:@"POST"];
    
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"Model.png"],  0.0);
    NSString *string = [NSString stringWithFormat:@"filepath=%@",_filePath];
    
    NSData *pathData = [string dataUsingEncoding:NSUTF8StringEncoding];
    [body appendData:pathData];
    
    //Image
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n",@"newFile.png"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:nil];
    NSLog(@"%@",dict);
    

    thanks

    解决方案

    You can't just do [body appendData:pathData]; at the start. You need to add it with the appropriate boundary and content information. So you should have a number of lines like:

    1. Add the boundary
    2. Add the content info
    3. Add the content data
    4. Add the boundary

    And repeat from 2 for each additional piece of data to be added.

    Check the spec for information on the appropriate content types and disposition info that needs to be added for each data type (perfect example for you is at the bottom).

    这篇关于使用两个参数iOS将文件发送到服务器iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆