如何在多部分表单中以附件形式发送多个文件[HTTP POST请求]

问题描述:

我接近终极的我的应用程序,最后一步是实现一个功能,我需要发送多个文件到一个消息。此外,我想模仿邮件附件行为像pict。

I am near to ultimate my app, and the final step is to implement a feature where I need to send multiple files to one message. Also I would like to imitate Mail attachment behavior like pict.

我已实现这

i have achieve this

但是,对于发送多个文件我有一个问题:只有第一个文件出现在接收到的消息;其他人失去了。

Instead, for sending multiple file I have one problem: only first file appears on received message; the others was lost.

因此,请查看我的代码,特别是循环循环,以寻找可能的错误和建议。感谢您的时间。

So please look my code, especially the cycle loop, for possible mistake and or suggestion. Thank you for your time.

- (IBAction)simple3:(id)sender{

NSLog(@"The document: %@", theDocument);


NSString* url = [theDocument absoluteString];
NSLog(@"The document: %@", url);


NSString* from = @"Excited Sundsx <mailgun@xvcvx.com>";
NSString* to = @"sxxxxx@gmail.com";
NSString* subject = @"Attach From Objective-c App";
NSString* text = @"Hello World";

//test
 NSImage *myImage = [[NSImage alloc]initWithContentsOfURL:theDocument];
 if (myImage != nil)
  {
    NSLog(@"Image seem to be ok");
  }else
    NSLog(@"Image seem to be wrong");

NSData *imageData;
NSString *image_name;
NSImage *image;

//-- Convert string into URL
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"https://api.mailgun.net/v3/bbbbbbb.me/messages"]];
NSString *authStr = @"api:key-00000000000000000000";
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData  base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

[request setHTTPMethod:@"POST"];

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

//-- Append data into posr url using following method
NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"from"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",from] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"to"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",to] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"subject"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",subject] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"text"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",text] dataUsingEncoding:NSUTF8StringEncoding]];


// here loop through mutableArray populated from openPal [openPanel URLs]
 lika as pict.

NSLog(@"ARRAYCOUNT: %lu", (unsigned long)filesArrayPath.count);
for (int y = 0; y < [filesArrayPath count]; y++) {
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     image_name = [filesArrayPath objectAtIndex:y];
     NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
     NSLog(@"added %i", y+1);

     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[NSData dataWithData:dataImg]];
 }

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

//-- Sending data into server through URL
[request setHTTPBody:body];

//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);


 }

...要附加在cUrl中的代码:

...for more information on what to do, I attach the code in cUrl:

 curl -s --user 'api:YOUR_API_KEY' \
 https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
 -F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
 -F to='foo@example.com' \
 -F cc='bar@example.com' \
 -F bcc='baz@example.com' \
 -F subject='Hello' \
 -F text='Testing some Mailgun awesomness!' \
 --form-string html='<html>HTML version of the body</html>' \
 -F attachment=@files/cartman.jpg \
 -F attachment=@files/cartman.png


damn累了!我为失去的时间道歉,但我已经找到解决我的问题两分钟后,我发布此帮助请求。解决方案:

damn tired ! I apologize for the time lost, but I've found the solution for my problem two minutes after i was post this help request. the solution:

for (int y = 0; y < [filesArrayPath count]; y++) {
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     NSString *image_name = [filesArrayPath objectAtIndex:y];
     NSData *dataImg = [[NSData alloc]initWithContentsOfURL:[filesArrayPath objectAtIndex:y]];
     NSLog(@"added %i", y+1);

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"attachment\"; filename=\"%@\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:dataImg]];
}