0

UIImageサーバーにアップロードしたい。このために、私は次のコード行を使用しています::

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{


imageView.image = image;    

NSData *pngData = UIImagePNGRepresentation(imageView.image);

NSString *imageFile = @"image.png";   
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [docDir stringByAppendingPathComponent:imageFile]; 
NSString *dataIS=[NSString base64StringFromData:pngData length:[pngData length]];
[pngData writeToFile:filePath atomically:YES];






NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];


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


NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:pngData]];
[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];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


[self.parentViewController dismissModalViewControllerAnimated:YES];

}

しかし、gdbで次のエラーが発生します::

Your request could not be processed. Request could not be handled

私はまた、それの適切な使用について混乱していますurlString。つまり、私はそれを正しく使用することに疑問を持っています。

ただし、以下urlStringを使用すると、画像ファイルをサーバーにアップロードできます::

NSString *urlString = [NSString stringWithFormat:@"http://xxxx.com/mediawiki/api.php?action=upload&filename=image.png&url=%@&token=%@", url, token] ;

APIfor (http://www.mediawiki.org/wiki/API:Upload)を参照して、mediawiki誰かが私がそれを整理するのを手伝ってくれる?よろしくお願いします。

4

4 に答える 4

2

複数の画像とデータをアップロードするのと同じ問題がありますが、AFNetworking クラスを使用し、複数の画像をサーバーにアップロードしています。

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"your URL"]];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
path = [[NSString alloc] initWithString:[URL absoluteString]];
vdict = @{
            // Your Parameter Pass
        };

[self checkmultiple:vdict];


-(void)checkmultiple:(NSDictionary*)vdict
{

AFHTTPRequestOperation *op = [manager POST:path parameters:vdict constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                              {
                                  for(i = 0;i<[appDelegate.gblarrydata count];i++)
                                  {
                                      NSString *paths = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
                                      // NSLog(@"%@",paths);
                                      NSString *documentsDirectory = paths;

                                      NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[[appDelegate.gblarrydata objectAtIndex:i]valueForKey:@"nameimage"]];

                                      NSData *data1 =[[NSData alloc]initWithContentsOfFile:getImagePath];
                                      NSString *data2 =[[appDelegate.gblarrydata objectAtIndex:i] valueForKey:@"Note"];

                                      [formData appendPartWithFileData:data1 name:[NSString stringWithFormat:@"pimage%d",i+1] fileName:[NSString stringWithFormat:@"pimage%d.jpg",i+1] mimeType:@"image/jpeg"];

                                      if(![data2 isEqualToString:@""])
                                      {
                                          [formData appendPartWithFormData:[data2 dataUsingEncoding:NSUTF8StringEncoding]
                                                                      name:[NSString stringWithFormat:@"pnotes%d",i+1]];
                                      }


                                  }


                              }
                             success:^(AFHTTPRequestOperation *operation, id responseObject)
                              {
                                  NSLog(@"Upload success");

                                  NSString *returnString = [NSString stringWithFormat:@"%@",[responseObject JSONRepresentation]];
                                  NSLog(@"Reg Date:%@",returnString);

                              }

                             failure:^(AFHTTPRequestOperation *operation, NSError *error)
                              {
                                  NSLog(@"Error:%@",error);
                              }];
[op start];

} 
于 2014-08-27T10:38:18.753 に答える
1

私も同じ問題を抱えていました。ファイルのアップロードにASIHTTPRequestを使用 しましたが、正常に動作します。

アップロード中:

画像ファイル:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

他のデータと同時に:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];

// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

目的のパスにダウンロードする場合:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.txt"];
于 2013-01-06T08:16:54.197 に答える
0

// UIImage * img = [UIImage imageNamed:@"min.png"];

// NSData *imageData = UIImageJPEGRepresentation(img,0.2);

NSString *strUrl =[NSString stringWithFormat:@"%@/upload_file.php", [globalApp sharedUser].vg_dominio ];
//strUrl = @"http://192.168.1.100/mismedicinas/upload_file.php";

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
request.HTTPMethod = @"POST";
request.timeoutInterval = 60;
request.HTTPShouldHandleCookies = false;

//[request setHTTPMethod:@"POST"];


NSString *boundary = @"----------SwIfTeRhTtPrEqUeStBoUnDaRy";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
//[request addValue:contentType forHTTPHeaderField:@"Content-Type"];



NSMutableData *body = [NSMutableData data];
NSMutableData *tempData = [NSMutableData data];

[tempData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[tempData appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphoneimage.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[tempData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//[tempData appendData:[NSData dataWithData:imageData]];  ///IMAGEN
[tempData appendData:[NSData dataWithData:cData]];

[tempData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData: tempData];


[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];



[request setValue: [NSString stringWithFormat:@"%d", body.length ] forHTTPHeaderField:@"Content-Length"];


request.HTTPBody =body;





NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"finalizacion %@", returnString);
于 2014-09-25T01:06:47.623 に答える
0

画像をサーバーに送信する場合、エンコードされた形式のデータで送信すると、サーバーはエンコードされた形式の画像を処理でき、エンコードされた画像データを取得します。

于 2012-07-18T09:08:58.573 に答える