2

我正在开发一个用于 IOS 应用程序的 API,其中用户将有一个包含三个字段的小表单 1. 文件类型,他可以只输入文本,例如图像或 pdf 2. 在第二个字段中,他可以浏览任何类型的 pdf 或图像像png,jpg等在他的单元格和3.Remarks:他可以在哪里发表一些评论。

填写完所有字段后,他将单击发送,管理员将收到带有图像附件的电子邮件。

现在,当我在数组中硬编码文件名并将该文件保存在服务器上时,我遇到了一个问题

  $files = array("myimage.png"); 

它工作正常,将它们附加到电子邮件中并发送,但我想让它像用户输入相关数据一样自动执行,而我的 api 会自动执行。

这是我到目前为止工作的代码

 <?php

if (is_uploaded_file($_FILES['file']['tmp_name'])) {
    echo "FILE NAME: ".$_FILES['file']['filename'];

// array with filenames to be sent as attachment
 //$files = array("1378326315_joomla.png");
   $files = $_FILES['file']['filename']; 
 //email fields: to, from, subject, and so on
   $to = "admin email";
   $from = "user name here"; 
   $subject ="this "; 
   $message = "My message";
   $headers = "From: $from";

// boundary 
   $semi_rand = md5(time()); 
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

  // headers for attachment 
  $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "        boundary=\"{$mime_boundary}\""; 

  // multipart boundary 
     $message = "This is a multi-part message in MIME format.\n\n" . "--   {$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-    Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
     $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
    } 

    // send

       $ok = @mail($to, $subject, $message, $headers); 
       if ($ok) { 
   echo "<p>mail sent to $to!</p>"; 
       } else { 
   echo "<p>mail could not be sent!</p>"; 
       } 


  } else {
  echo "Nothing";
}


?>

我可以轻松地在 php for web 中处理这整个功能,但我对 IOS 感到困惑,因为它是我的第一个 API 以及与我手机的交互。希望我的问题很清楚,如果需要,我一定会提供更多细节,但我错过了。提前感谢您的帮助。

编辑:我还想补充一点,不需要根据要求在数据库中保存任何数据,但是我应该在发送之前先将文件保存在服务器上的任何文件夹中吗?

4

1 回答 1

0
-(NSURLRequest *)createRequestWithPath:(NSString *)path withKeys:(NSDictionary *)postKeys withData:(NSData *)data

{ //创建到 tumblr 的 URL POST 请求

NSString *urlStr = [NSString stringWithFormat:@"http://192.168.1.28/index.php?/%@",path];

NSURL *tumblrURL = [NSURL URLWithString:urlStr];
NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL];
[tumblrPost setHTTPMethod:@"POST"];

//Add the header info
//NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *stringBoundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"];

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

//add key values from the NSDictionary object

if (![postKeys isEqual:nil]) 
{
    NSEnumerator *keys = [postKeys keyEnumerator];
    int i;
    for (i = 0; i < [postKeys count]; i++) {
        NSString *tempKey = [keys nextObject];
        [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
}

if (![data isEqual:nil])
{
    NSLog(@"data size : %i",[data length]);
    //add data field and file data
    [postBody appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[NSData dataWithData:data]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}

self.dataSize = [postBody length];
//add the body to the post
[tumblrPost setHTTPBody:postBody];
return tumblrPost;

}

还要查找NSURLConnectionDataDelegate

于 2013-09-14T08:28:08.723 回答