如何以多部分形式发送多个文件作为附件 [HTTP POST 请求]

How to Send Multiple File as attachment in Multipart Forms [HTTP POST Request]

我的应用程序即将完成,最后一步是实现一项功能,我需要将多个文件发送到一条消息。我也想模仿像 pict 这样的邮件附件行为。

我已经做到了

相反,对于发送多个文件,我有一个问题:只有第一个文件出现在收到的消息中;其他人迷路了。

因此,请查看我的代码,尤其是循环循环,以了解可能存在的错误和/或建议。谢谢你的时间。

- (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 中:

 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

太累了!对于耽误的时间,我深表歉意,但我在 post 这个帮助请求两分钟后就找到了问题的解决方案。解决方案:

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]];
}