首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将多个镜像上传到服务器IOS

将多个镜像上传到服务器IOS
EN

Stack Overflow用户
提问于 2015-06-12 13:47:03
回答 1查看 3.7K关注 0票数 1

我想知道如何使用objective C将多个图像上传到服务器。以下是我尝试使用的代码片段。

代码语言:javascript
复制
-(void)uploadImage:(NSMutableArray *)image andMessageBO:(MessageBO *)message
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@PostImageServlet?",kPostImageUploadWebServiceURL]];
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:url];

    [postRequest setHTTPMethod:@"POST"];
    [postRequest setTimeoutInterval:60.0];
    NSString *stringBoundary = @"0xKhTmLbOuNdArY";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
    [postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSData *imageData =UIImagePNGRepresentation(image);


    NSMutableData *postBody = [[NSMutableData alloc] init];
    [postBody appendData:[[NSString stringWithFormat:@"isFormField=true"]dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%d",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profilepic\"; filename=\"%d_postImage.png\"\r\n",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
       for (int i = 0; i < [image count]; i++)
    {
        [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profilepic\"; filename=\"%d_postImage%d.png\"\r\n",messageBO.messageId,i] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[image objectAtIndex:i]];
    }
    [postBody appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [postBody appendData:[NSData dataWithData:imageData]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postRequest setHTTPBody:postBody];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    DebugLog(@"IMge:%@",returnString);

    //  [self removeOfflineImage:expenseBO];

}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-06-12 14:38:48

我个人使用这种方法与员工交流,并将员工上传到服务器。这是一种json类型的方法:

代码语言:javascript
复制
- (NSURLRequest *)convertToRequest:(NSString *)stringURL withDictionary:(NSDictionary *)dictionary
{

    NSError *error = nil;

    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];

    NSURL *url = [NSURL URLWithString:stringURL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: JSONData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept-Encoding"];

    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[JSONData length]] forHTTPHeaderField:@"Content-Length"];


    return request;
}

字典参数填充的内容如下所示:

首先将数据转换为base64String格式,如下所示:

代码语言:javascript
复制
NSData *data = UIImageJPEGRepresentation(self.userImageView.image, 1.0);

NSData *imageBase64Data = [data base64EncodedDataWithOptions:0];

NSString *imageBase64String = [[NSString alloc] initWithData:imageBase64Data encoding: NSUTF8StringEncoding];

将此内容添加到字典...

代码语言:javascript
复制
NSDictionary *jsonDictionary = @{
                                   @0 : @{
                                     "name": "file1",
                                     "image" : imageBase64String1
                                   },
                                   @1 : @{
                                     "name": "file2",
                                     "image" : imageBase64String2
                                   } //and so on.. 
                                }; 
//you may want to put that in a loop

[ImplementationClass convertToRequest:YourServerURL withDictionary: jsonDictionary];

在服务器端,类似于:

对发送到服务器的数据进行解码(PHP):

代码语言:javascript
复制
// Retrieves data

$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$json_decoded = json_decode($jsonInput,true);

/* 
* I commented this because i'm not sure if this suits my `NSDictionary` above
* but you can always check by logging `$json_decoded` or probably `var_dump()` function of php
*
* $json_decoded[0]['name'];
* $json_decoded[0]['image'];
* $json_decoded[1]['name'];
* $json_decoded[1]['image'];
*/

function upload_image($filename, $uploadedfile) {

    $save_file_path = getcwd()."/uploads/";

    $save_file_path     .= $filename;

    $image_file         = base64_decode($uploadedfile);

    //DELETES EXISTING
    if (file_exists($save_file_path)) {  unlink($save_file_path);  } 

    //CREATE NEW FILE
    file_put_contents($save_file_path, $image_file); 

    //CHECK FILE IF EXIST 
    return ((file_exists($save_file_path)) ? true : false );

}

希望这对你有帮助..干杯..

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30796059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档