Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

iphone - To upload the image file to a web form

I do have some image files(.png files) in document directory(iPhone). I am viewing web forms(.aspx) on the UIWebView. when I click on the submit button of the form, i just want to attach the image file to the form and wants to send that file to the web server along with the web form.

The thing I m suffering from is, i am not getting how to attach an image file to the web form and how to submit those two.

Kindly help me out of this. I am not getting any of the hint how to do it.

Thanking you....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will need to modify these examples to work for you, but they are working examples.

Objective-C

MyViewController.m:

- (void) upload
{
    NSString *urlString = @"http://www.yourwebsite.com/uploads/upload.php";
    NSData *data = /* turn your png into NSData */

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /* now lets create the body of the post */
    NSString *content = [NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@.png"
",@"yourPng"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:content] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: text/plain

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:data]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
}

PHP

upload.php:

<?php
//assuming upload.php and upload folder are in the same dir
$uploaddir = 'uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
$product = $_GET[product];

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
    echo "PNG uploaded. 
";
} else {
    echo "PNG not uploaded. 
";
}

if ($_FILES['userfile']['size']> 300000)     //Limiting image at 300K
{
    exit("Your file is too large."); 
}

// Add support here for PNG files:
if ((!($_FILES['userfile']['type'] == "text/plain")) &&  //Also allowing 
    (!($_FILES['userfile']['type'] == "text/plist")) &&  //plist files
    (!($_FILES['userfile']['type'] == "text/html")))     //HTML files
{
    exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you uploaded."); 
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $postsize = ini_get('post_max_size');    //Not necessary, I was using these
    $canupload = ini_get('file_uploads');    //server variables to see what was 
    $tempdir = ini_get('upload_tmp_dir');    //going wrong.
    $maxsize = ini_get('upload_max_filesize');
    echo "http://www.yourwebsite.com/uploads/{$file}" . "
" . $_FILES['userfile']['size'] . "
" . $_FILES['userfile']['type'] ;
}
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...