<small id='ww04d'></small><noframes id='ww04d'>

        <legend id='ww04d'><style id='ww04d'><dir id='ww04d'><q id='ww04d'></q></dir></style></legend>
          <bdo id='ww04d'></bdo><ul id='ww04d'></ul>

      1. <i id='ww04d'><tr id='ww04d'><dt id='ww04d'><q id='ww04d'><span id='ww04d'><b id='ww04d'><form id='ww04d'><ins id='ww04d'></ins><ul id='ww04d'></ul><sub id='ww04d'></sub></form><legend id='ww04d'></legend><bdo id='ww04d'><pre id='ww04d'><center id='ww04d'></center></pre></bdo></b><th id='ww04d'></th></span></q></dt></tr></i><div id='ww04d'><tfoot id='ww04d'></tfoot><dl id='ww04d'><fieldset id='ww04d'></fieldset></dl></div>
      2. <tfoot id='ww04d'></tfoot>

        将图像从 iOS 应用程序上传到 php --- 不能完全正确 --- 我错过了什么?

        时间:2023-12-01

        <small id='jSC7X'></small><noframes id='jSC7X'>

      3. <legend id='jSC7X'><style id='jSC7X'><dir id='jSC7X'><q id='jSC7X'></q></dir></style></legend>
          <tbody id='jSC7X'></tbody>
        <tfoot id='jSC7X'></tfoot>

            <bdo id='jSC7X'></bdo><ul id='jSC7X'></ul>
              <i id='jSC7X'><tr id='jSC7X'><dt id='jSC7X'><q id='jSC7X'><span id='jSC7X'><b id='jSC7X'><form id='jSC7X'><ins id='jSC7X'></ins><ul id='jSC7X'></ul><sub id='jSC7X'></sub></form><legend id='jSC7X'></legend><bdo id='jSC7X'><pre id='jSC7X'><center id='jSC7X'></center></pre></bdo></b><th id='jSC7X'></th></span></q></dt></tr></i><div id='jSC7X'><tfoot id='jSC7X'></tfoot><dl id='jSC7X'><fieldset id='jSC7X'></fieldset></dl></div>

                  本文介绍了将图像从 iOS 应用程序上传到 php --- 不能完全正确 --- 我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  首先,我知道这个问题已经被问了一千次了.我再次问,因为我已经尝试了其他示例中的解决方案,但它们对我不起作用,我不知道为什么.每个人的方法似乎略有不同.

                  Firstly, I know this question has been asked a thousand times. I'm asking again because I've tried the solutions in the other examples and they are not working for me and I don't know why. Everyone seems to have a slightly different approach.

                  NSData *imageData =  UIImagePNGRepresentation(form.image);
                  NSURL *url = [NSURL URLWithString:@"myscript.php"];
                  NSMutableString *postParams = [[NSMutableString alloc] initWithFormat:@"&image=%@", imageData]];
                  
                  NSData *postData = [postParams dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
                  NSString *postLength = [[NSString alloc] initWithFormat:@"%d", [postData length]];
                  
                  NSMutableURLRequest *connectRequest = [[NSMutableURLRequest alloc] init];
                  [connectRequest setURL:url];
                  [connectRequest setHTTPMethod:@"POST"];
                  [connectRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
                  [connectRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                  //[connectRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
                  [connectRequest setHTTPBody:postData];
                  
                  NSData *receivedData;
                  NSDictionary *jsonData;
                  
                  NSURLConnection *connectConnection = [[NSURLConnection alloc] initWithRequest:connectRequest delegate:self];
                  
                  NSError *error = nil;
                  
                  if (!connectConnection) {
                      receivedData = nil;
                      NSLog(@"The connection failed!");
                  } else {
                      NSLog(@"Connected!");
                      receivedData = [NSURLConnection sendSynchronousRequest:connectRequest returningResponse:NULL error:&error];
                  }
                  
                  if (!receivedData) {
                      NSLog(@"Data fetch failed: %@", [error localizedDescription]);
                  } else {
                      NSLog(@"The data is %lu bytes", (unsigned long)[receivedData length]);
                      NSLog(@"%@", receivedData);
                  
                      if (NSClassFromString(@"NSJSONSerialization")) {
                          id object = [NSJSONSerialization JSONObjectWithData:receivedData options:0 error:&error];
                  
                          if (!object) {
                              NSLog(@"JSON Serialization failed: %@", [error localizedDescription]);
                          }
                  
                          if ([object isKindOfClass:[NSDictionary class]]) {
                              jsonData = object;
                              NSLog(@"json data: %@", jsonData);
                          }
                      }
                  }
                  

                  目前我在 postParams 中传递 NSData 并使用这个 php 脚本:

                  At the moment I am passing the NSData in the postParams and using this php script:

                  if (isset($_POST['image']) && !empty($_POST['image'])) {
                  
                       if (file_put_contents('images/test.png', $_POST['image'])) {
                             echo '{"saved":"YES"}'; die();
                       } else {
                             echo '{"saved":"NO"}'; die();     
                       }
                  }
                  

                  这是将数据保存到文件中,但我无法打开它,因为它已损坏或诸如此类.这几乎是最后的努力,我真的没想到它会以这种方式工作,但它与我到目前为止所做的一样接近.

                  This is saving the data to a file but I can't open it as it is corrupted or some such thing. This was pretty much a last ditch effort and I didn't really expect it to work this way but it's as close as I've come so far to getting it right.

                  我尝试过使用各种内容标头/边界/$_FILES/enctype 内容类型方法,但我什至无法将其正确发送到脚本.

                  I've tried using various content header/ boundary / $_FILES / enctype content-type methods but I can't even get it to send to the script properly like that.

                  • 顺便说一句,我不只是发送图像数据,我还在 postParams 中发布其他值,这些值只是字符串、整数等.

                  有没有人对此有任何建议或知道任何好的资源?

                  Does anyone have any suggestions or know of any good sources out there for this?

                  感谢您提供的任何帮助.

                  Thanks for any assistance offered.

                  遵循以下答案中给出的建议后的当前状态(以及来自程序其他部分的更多信息):

                  Current state after following advice given in answers below (also, further information from other parts of program):

                  图像的初始捕获:

                  - (void)viewWillDisappear:(BOOL)animated
                  {
                      [super viewWillDisappear:animated];
                  
                      [self.view endEditing:YES];
                  
                      __unused form *form = self.form;
                  
                      form.signature = self.signatureDrawView.bp;
                  
                      UIGraphicsBeginImageContext(self.signatureDrawView.bounds.size);
                      [self.signatureDrawView.layer renderInContext:UIGraphicsGetCurrentContext()];
                      campaignForm.signatureImage = UIGraphicsGetImageFromCurrentImageContext();
                      UIGraphicsEndImageContext();
                  }
                  

                  其中 signatureDrawView 是 UIView,form.signature 是 UIBezierpath.

                  where the signatureDrawView is a UIView and the form.signature is a UIBezierpath.

                  那么……

                  NSData *sigImage =  UIImagePNGRepresentation(campaignForm.signatureImage);
                  

                  传递给以下函数:

                  - (void)uploadImage:(NSData *)imageData
                  {
                      NSMutableURLRequest *request;
                      NSString *urlString = @"https://.../upload.php";
                      NSString *filename = @"uploadTest";
                      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"];
                      NSMutableData *postbody = [NSMutableData data];
                      [postbody appendData:[[NSString stringWithFormat:@"
                  --%@
                  ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                      [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="file"; filename="%@.png"
                  ", filename] dataUsingEncoding:NSUTF8StringEncoding]];
                      [postbody appendData:[@"Content-Type: application/octet-stream
                  
                  " dataUsingEncoding:NSUTF8StringEncoding]];
                      [postbody appendData:[NSData dataWithData:imageData]];
                      [postbody appendData:[[NSString stringWithFormat:@"
                  --%@--
                  ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                      [request setHTTPBody:postbody];
                  
                      NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                      NSString *returnString;
                      returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                      NSLog(@"%@", returnString);
                  }
                  

                  upload.php 看起来像:

                  upload.php looking like:

                      if ($_FILES["file"]["error"] > 0) {
                          echo '{"file":"'.$_FILES['file']['error'].'"}';
                          die();
                      } else {
                          $size = $_FILES["file"]["size"] / 1024;
                          $upload_array = array(
                                      "Upload"=>$_FILES["file"]["name"],
                                      "Type"=>$_FILES["file"]["type"],
                                      "Size"=>$size,
                                      "Stored in"=>$_FILES["file"]["tmp_name"]
                                      );
                          //echo json_encode($upload_array);
                          if (move_uploaded_file($_FILES["file"]["tmp_name"], "signatures/" . $_FILES["file"]["name"])) {
                              echo '{"success":"YES"}';
                              die();  
                          } else { 
                              echo '{"success":"NO"}';
                              die();  
                          }
                          die();
                      }
                  

                  这给了我 {success:NO} 输出,并且 $upload_array 转储显示空值.

                  This is giving me the {success:NO} output and the $upload_array dump shows null values.

                  推荐答案

                  下面的代码,求帮助

                  NSData *myData=UIImagePNGRepresentation([self.img image]);
                  NSMutableURLRequest *request;
                  NSString *urlString = @"http://xyzabc.com/iphone/upload.php";
                  NSString *filename = @"filename";
                  request= [[[NSMutableURLRequest alloc] init] autorelease];
                  [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"];
                  NSMutableData *postbody = [NSMutableData data];
                  [postbody appendData:[[NSString stringWithFormat:@"
                  --%@
                  ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@.jpg"
                  ", filename] dataUsingEncoding:NSUTF8StringEncoding]];
                  [postbody appendData:[@"Content-Type: application/octet-stream
                  
                  " dataUsingEncoding:NSUTF8StringEncoding]];
                  [postbody appendData:[NSData dataWithData:myData]];
                  [postbody appendData:[[NSString stringWithFormat:@"
                  --%@--
                  ",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [request setHTTPBody:postbody];
                  
                  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                  NSString *returnString;
                  returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                  NSLog(@"%@", returnString);
                  

                  这篇关于将图像从 iOS 应用程序上传到 php --- 不能完全正确 --- 我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法在 codeigniter 中保存 1 个表单中的 2 个文件 下一篇:如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能

                  相关文章

                  <i id='89AX5'><tr id='89AX5'><dt id='89AX5'><q id='89AX5'><span id='89AX5'><b id='89AX5'><form id='89AX5'><ins id='89AX5'></ins><ul id='89AX5'></ul><sub id='89AX5'></sub></form><legend id='89AX5'></legend><bdo id='89AX5'><pre id='89AX5'><center id='89AX5'></center></pre></bdo></b><th id='89AX5'></th></span></q></dt></tr></i><div id='89AX5'><tfoot id='89AX5'></tfoot><dl id='89AX5'><fieldset id='89AX5'></fieldset></dl></div>
                  <legend id='89AX5'><style id='89AX5'><dir id='89AX5'><q id='89AX5'></q></dir></style></legend>
                  <tfoot id='89AX5'></tfoot>

                  1. <small id='89AX5'></small><noframes id='89AX5'>

                        <bdo id='89AX5'></bdo><ul id='89AX5'></ul>