Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将两个图像上载到Web服务器_Ios_Objective C_Post_File Upload_Afnetworking - Fatal编程技术网

Ios 将两个图像上载到Web服务器

Ios 将两个图像上载到Web服务器,ios,objective-c,post,file-upload,afnetworking,Ios,Objective C,Post,File Upload,Afnetworking,我正试图上传两张图片到我的网络服务器。下面是我现在要做的,上传一张图片: NSData *imageData = UIImagePNGRepresentation(imageToSend); // setting up the URL to post to NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php"; // setting up the request object now NSMutable

我正试图上传两张图片到我的网络服务器。下面是我现在要做的,上传一张图片:

NSData *imageData = UIImagePNGRepresentation(imageToSend);
// setting up the URL to post to
NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php";

// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

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

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

//Create the connection with the string URL and kick it off
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[urlConnection start];
一张图片就可以了,但是,我怎么能同时发送两张图片呢

一个选择是使用AFNetworking,但目前为止我无法让它工作,以下是我尝试的:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:urlString];
NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"ipodfile.jpg" mimeType:@"image/jpeg"];
}];

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:requestHTTP];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    //CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100;


}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Upload failed");
}];
[operation start];
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:urlString];
NSMutableURLRequest*requestHTTP=[httpClient multipartFormRequestWithMethod:@“POST”路径:@“/”参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:imageData名称:@“userfile”文件名:@“ipodfile.jpg”mimeType:@“image/jpeg”];
}];
AFJSONRequestOperation*操作=[[AFJSONRequestOperation alloc]initWithRequest:requestHTTP];
[操作设置LoadProgressBlock:^(NSUTEGER字节写入、long long TotalBytesWrite、long long totalBytesExpectedToWrite){
NSLog(@“已发送%lld个字节,共%lld个字节”,TotalBytesWrite,totalBytesExpectedToWrite);
//CGFloat进度=((CGFloat)TotalBytesWrite)/totalBytesExpectedToWrite*100;
}];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(“上传成功”);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“上传失败”);
}];
[操作启动];
以下是我的php代码:

<?php
    $currentDirectory = getcwd(); 
    $uploaddir = $currentDirectory."/4/HD/";
    $uploaddir2 = $currentDirectory."/4/thumb/";
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    $file2 = basename($_FILES['userfile2']['name2']);
    $uploadfile2 = $uploaddir2 . $file2;

  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo $uploaddir."{$file}";
      } 

  if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) {
        echo $uploaddir2."{$file2}";
      } 
?>

只需在请求中添加另一个具有不同名称的图像即可

 NSMutableData *body = [NSMutableData data];
   [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[NSData dataWithData:imageData]];
   [body appendData:[[NSString stringWithFormat:@"\r\n",boundary]  dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE
    //and add

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"ipodfile2.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
在添加两个文件之前,不要关闭正文--%--\r\n

[编辑]我就是这样做的:

if([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSData *data = [NSData dataWithContentsOfFile:path];
        //NSLog(@"image present:%d",[data length]);

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Disposition: attachment; name=\"file1\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        //[body appendData:[self encryptThisData:data]];
        [body appendData:data];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    if([[NSFileManager defaultManager] fileExistsAtPath:videoPath])
    {
        NSString *content = [NSString stringWithContentsOfFile:videoPath encoding:NSUTF8StringEncoding error:nil];
        NSURL *pathURL = [NSURL URLWithString:content];
        ////NSLog(@"cur url is:%@",pathURL);
        NSData *data = [NSData dataWithContentsOfURL:pathURL];
        //NSLog(@"data len is:%d",[data length]);

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Disposition: attachment; name=\"file3\"; filename=\"video.mov\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:data];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
// close form   
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body 
    [request setHTTPBody:body];

为什么不在同一请求中添加另一个图像呢?行
[body appendData:[[NSString stringWithFormat:@“\r\n”,boundary]dataUsingEncoding:NSUTF8StringEncoding]]//MOD HERE
shuld be
[body appendData:[NSString stringWithFormat:@“\r\n--%@”,boundary]数据使用编码:NSUTF8StringEncoding]//这里的MOD
不是吗@加布卡斯