Send files from iphone

  • I need to be able to upload the data from a UIImage to a server via
    http POST however I simply cannot find a good example of how to
    arrange the headers or how to make this post.
    The only data I need to send to the server is the file's name, the
    user's name and the data itself and I simply cannot seem to get it to
    work. Can any of you offer a helping hand?
  • What have you tried? Or at least looked at trying?
  • I have been trying to write my headers for multipart form data. I'm
    then sending the data over a NSStream. If it would be easier however
    to compact this somehow into a url request that would work fine too. I
    just need to figure out how to get the file to the server.

    On Jul 3, 2009, at 7:31 PM, Kyle Sluder wrote:

    > What have you tried? Or at least looked at trying?
  • Check out documentation on NSURLRequest and NSURLConnection.

    Luke

    Sent from my iPhone.

    On Jul 3, 2009, at 7:20 PM, Development <development...>
    wrote:

    > I need to be able to upload the data from a UIImage to a server via
    > http POST however I simply cannot find a good example of how to
    > arrange the headers or how to make this post.
    > The only data I need to send to the server is the file's name, the
    > user's name and the data itself and I simply cannot seem to get it
    > to work. Can any of you offer a helping hand?
  • I also tried this and it fails. The form never gets any post variables
    or files:

    [self sendText:@"Content-Type: multipart/form-data; boundry=@@##$$-
    rew-**&&^%^\n"];
    [self sendText:boundry];
    [self sendText:[NSString stringWithFormat:@"Content-Length: %i\n
    \n",totalSize]];
    [self sendText:[NSString stringWithFormat:@"Content-Disposition: form-
    data; name=\"%@\"\r\n\r\n",@"fileName"]];
    [self sendText:[NSString stringWithFormat:@"%@",name]];
    [self sendText:boundry];
    [self sendText:[NSString stringWithFormat:@"Content-Disposition: form-
    data; name=\"%@\"\r\n\r\n",@"directory"]];
    [self sendText:[NSString stringWithFormat:@"%@",dir]];
    [self sendText:boundry];
    [self sendText:[NSString stringWithFormat:@"Content-Disposition: form-
    data; name=\"imagefile\" filename=\"%@\"\r\n",name]];
    [self sendText:@"Content-Type: application/octet-stream\r\n\r\n"];
    [self sendData:imageData];
    [self sendText:boundry];

    On Jul 3, 2009, at 8:00 PM, Luke Hiesterman wrote:

    > Check out documentation on NSURLRequest and NSURLConnection.
    >
    > Luke
    >
    > Sent from my iPhone.
    >
    > On Jul 3, 2009, at 7:20 PM, Development
    > <development...> wrote:
    >
    >> I need to be able to upload the data from a UIImage to a server via
    >> http POST however I simply cannot find a good example of how to
    >> arrange the headers or how to make this post.
    >> The only data I need to send to the server is the file's name, the
    >> user's name and the data itself and I simply cannot seem to get it
    >> to work. Can any of you offer a helping hand?
  • Ok I have made a tiny bit of progress.

    The problem is that the _POST and _FILE arrays are still empty Well...
    the _POST array on the server is filled with a mix of the data it
    should have scrambled in with jpeg data. I desperately need help I
    cannot find the answer for this for some reason. I've been googling
    for hours now and the example code doesn't work either.

    Below is the sending code:

    NSMutableData * sendData= [[NSMutableData alloc]init];
    NSMutableData * body= [[NSMutableData alloc]init];
    NSString * boundry = @"@@##$$rew**&&^%^";
    NSString * name=[path lastPathComponent];
    NSString * dir= [path stringByDeletingLastPathComponent];

    [body appendData:[[NSString stringWithFormat:@"--%@\r
    \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    form-data; name=\"%@\"\r\n\r
    \n",@"fileName"]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString
    stringWithFormat:@"%@",name]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
    \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    form-data; name=\"%@\"\r\n\r
    \n",@"directory"]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString
    stringWithFormat:@"%@",dir]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
    \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    form-data; name=\"image_file\" filename=\"%@\"\r\n\r
    \n",name]dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n"
    dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r
    \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];

    int totalSize =[body length];
    NSLog(@"Total size: %i",totalSize);
    [sendData appendData:[@"Content-Type: multipart/form-data;
    boundary=@@##$$rew**&&^%^\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [sendData appendData:[[NSString stringWithFormat:@"Content-Length: %i
    \r\n\r\n",totalSize]dataUsingEncoding:NSUTF8StringEncoding]];
    [sendData appendData:body];
    [self startConnection];
    [self sendMainHeadersForPage:@"/image.php"];
    [self sendData:sendData];
  • It's the smallest errors that create the greatest frustration. I left
    out a semi colon. and misspelled boundary. Which makes me wonder why
    the examples I found didnt work.

    On Jul 3, 2009, at 9:34 PM, Development wrote:

    > Ok I have made a tiny bit of progress.
    >
    > The problem is that the _POST and _FILE arrays are still empty
    > Well... the _POST array on the server is filled with a mix of the
    > data it should have scrambled in with jpeg data. I desperately need
    > help I cannot find the answer for this for some reason. I've been
    > googling for hours now and the example code doesn't work either.
    >
    > Below is the sending code:
    >
    > NSMutableData * sendData= [[NSMutableData alloc]init];
    > NSMutableData * body= [[NSMutableData alloc]init];
    > NSString * boundry = @"@@##$$rew**&&^%^";
    > NSString * name=[path lastPathComponent];
    > NSString * dir= [path stringByDeletingLastPathComponent];
    >
    > [body appendData:[[NSString stringWithFormat:@"--%@\r
    > \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    > form-data; name=\"%@\"\r\n\r
    > \n",@"fileName"]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString
    > stringWithFormat:@"%@",name]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
    > \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    > form-data; name=\"%@\"\r\n\r
    > \n",@"directory"]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString
    > stringWithFormat:@"%@",dir]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r
    > \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[[NSString stringWithFormat:@"Content-Disposition:
    > form-data; name=\"image_file\" filename=\"%@\"\r\n\r
    > \n",name]dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:[@"Content-Type: application/octet-stream\r\n"
    > dataUsingEncoding:NSUTF8StringEncoding]];
    > [body appendData:imageData];
    > [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r
    > \n",boundry]dataUsingEncoding:NSUTF8StringEncoding]];
    >
    > int totalSize =[body length];
    > NSLog(@"Total size: %i",totalSize);
    > [sendData appendData:[@"Content-Type: multipart/form-data;
    > boundary=@@##$$rew**&&^%^\r\n"
    > dataUsingEncoding:NSUTF8StringEncoding]];
    > [sendData appendData:[[NSString stringWithFormat:@"Content-Length:
    > %i\r\n\r\n",totalSize]dataUsingEncoding:NSUTF8StringEncoding]];
    > [sendData appendData:body];
    > [self startConnection];
    > [self sendMainHeadersForPage:@"/image.php"];
    > [self sendData:sendData];
  • Hello Development,

    Saturday, July 4, 2009, 3:20:45 AM, you wrote:

    > I need to be able to upload the data from a UIImage to a server via
    > http POST however I simply cannot find a good example of how to
    > arrange the headers or how to make this post.
    > The only data I need to send to the server is the file's name, the
    > user's name and the data itself and I simply cannot seem to get it to
    > work. Can any of you offer a helping hand?

    I don't know if CoreFoundation stuff is available on iPhone but if it is, CFHTTPMessage is an excellent way to do HTTP POST.

    Best regards,
    Peter                            mailto:<darkmatter...>
  • I'm not quite sure about whether "Available in Mac OS X version 10.1
    and later" means that it's not available in iPhone OS.

    But, I assume everything that exists on iPhone OS reference library,
    exists on iPhone OS, because that's also the case with
    NSURLConnection, which obviously exists on iPhone OS.

    Jesse Armand
    ----------------------------------------
    (http://jessearmand.com)

    On Sat, Jul 4, 2009 at 6:58 PM, Peter
    Mulholland<darkmatter...> wrote:
    > Hello Development,
    >
    > Saturday, July 4, 2009, 3:20:45 AM, you wrote:
    >
    >> I need to be able to upload the data from a UIImage to a server via
    >> http POST however I simply cannot find a good example of how to
    >> arrange the headers or how to make this post.
    >> The only data I need to send to the server is the file's name, the
    >> user's name and the data itself and I simply cannot seem to get it to
    >> work. Can any of you offer a helping hand?
    >
    >
    > I don't know if CoreFoundation stuff is available on iPhone but if it is, CFHTTPMessage is an excellent way to do HTTP POST.
    >
    > Best regards,
    >  Peter                            mailto:<darkmatter...>
    >
  • CoreFoundation exists on the iPhone. And you can look at cocoadev.org
    for a decent HTTP Post example there or look on Google code for a
    class that does Flickr uploads through HTTP Post and that works on the
    iPhone.

    HTTP Post is not hard, but it does expect a very specific way of doing
    it.

    On Jul 4, 2009, at 7:46 AM, Jesse Armand wrote:

    > I'm not quite sure about whether "Available in Mac OS X version 10.1
    > and later" means that it's not available in iPhone OS.
    >
    > But, I assume everything that exists on iPhone OS reference library,
    > exists on iPhone OS, because that's also the case with
    > NSURLConnection, which obviously exists on iPhone OS.
    >
    > Jesse Armand
    > ----------------------------------------
    > (http://jessearmand.com)
    >
    >
    >
    > On Sat, Jul 4, 2009 at 6:58 PM, Peter
    > Mulholland<darkmatter...> wrote:
    >> Hello Development,
    >>
    >> Saturday, July 4, 2009, 3:20:45 AM, you wrote:
    >>
    >>> I need to be able to upload the data from a UIImage to a server via
    >>> http POST however I simply cannot find a good example of how to
    >>> arrange the headers or how to make this post.
    >>> The only data I need to send to the server is the file's name, the
    >>> user's name and the data itself and I simply cannot seem to get it
    >>> to
    >>> work. Can any of you offer a helping hand?
    >>
    >>
    >> I don't know if CoreFoundation stuff is available on iPhone but if
    >> it is, CFHTTPMessage is an excellent way to do HTTP POST.
    >>
    >> Best regards,
    >> Peter                            mailto:<darkmatter...>
    >>


    Alex Kac - President and Founder
    Web Information Solutions, Inc.

    "The optimist proclaims that we live in the best of all possible
    worlds; and the pessimist fears this is true."
    -- James Clabell