iphone SDK : Upload image from iphone to a php Server send empty file ?(sample code link inside)

11,326

Solution 1

use HTTP multipart POST or ASIHTTPRequest


@WebberLai

Save the image to home directory:
 NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
    UIImage *img = pickerImage;


// Write a UIImage to JPEG with minimum compression (best quality) 
    [UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

    NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

    request = [ASIFormDataRequest requestWithURL:webServiceUrl];
    [request setPostValue:requestString forKey:@"data"];
    [request setFile:photoPath forKey:@"apiupload"];
    [request startSynchronous];

Solution 2

It's fine to add this

NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"]; UIImage *img = pickerImage;

[UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];

But When I try to put in my button action

NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];

request = [ASIFormDataRequest requestWithURL:webServiceUrl];
[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];
[request startSynchronous];

It got many error message ,I think is delegate problem

first complier says "request undeclared" and "ASIFormDataRequest undeclared"

and "requestString undeclared"

I try to give request and requestString declared

So I use ASIFormDataRequest *request; and NSString *requestString;

It clear an error about "equestString undeclared"

I don't know how to declared ASIFormDataRequest so it still a error...

Did I right to put this in IBAction ???

I look some example here

Thanks again~you are a really nice guy !

More Edit :

OK, Here is more step about how to import necessary .h .m and framework

download here

and import this header :

ASIHTTPRequestConfig.h

ASIHTTPRequestDelegate.h

ASIProgressDelegate.h

ASICacheDelegate.h

ASIInputStream.h

ASIInputStream.m

ASIHTTPRequest.h

ASIHTTPRequest.m

ASIFormDataRequest.h

ASIFormDataRequest.m

ASINetworkQueue.h

ASINetworkQueue.m

ASIDownloadCache.h

ASIDownloadCache.m

iPhone projects must also include:

ASIAuthenticationDialog.h

ASIAuthenticationDialog.m

Reachability.h (in the External/Reachability folder)

Reachability.m (in the External/Reachability folder)

than import Framework:

CFNetwork

SystemConfiguration.framework

MobileCoreServices.framework

CoreGraphics.framework

libz.1.2.3.dylib

Share:
11,326
Webber Lai
Author by

Webber Lai

A new iphone programmer~ start my first code job from 2010/8

Updated on June 28, 2022

Comments

  • Webber Lai
    Webber Lai almost 2 years

    I try to send a photo and GPS location to server via PHP

    here is the PHP part:Copy from here Saving the Uploaded File

    The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.

    The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:

    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    

    The script above checks if the file already exists, if it does not, it copies the file to the specified folder.

    Note: This example saves the file to a new folder called "upload"

    now back to iOS part ,I only can send GPS location to server ,it's easy because just a string.

    and you can download sample code here

    But an image is not,I do some search and found this discussion

    I directly put the code in my send GPS&image button

    the best thing is it can feedback I send the file success or failed

    this is the most message I got after I send photo to server

    1.Invalid file

    2.Return Code:
    Upload:
    Type:
    Size: 0 Kb
    Temp file:
    Stored in: upload/

    I follow some guide to save image to NSData

    NSData *photoData= UIImageJPEGRepresentation(self.theimageView.image, 1.0);
    

    and I also check there is data inside or not

    so simply add this code into Button action

    if (photoData == nil) {
        NSLog(@"The photo is nothing !!!");
    }
    else {
        NSLog(@"Photo inside !!!!");
    

    It works fine.......

    But How To Upload An Image To Server ???

    maybe I need to give a file name then upload it ???

    but how to...

    I never use camera function and upload data before

    Hope someone can figure out this problem

    Many Great Thanks !!!