Iphone 在同一帖子中上载图像和其他数据

Iphone 在同一帖子中上载图像和其他数据,iphone,image,post,Iphone,Image,Post,我想用POST方法上传图片。我可以分别上传图片。但是我想把它们和我需要发送的其他数据一起发布到服务器上。谁能帮我一下吗?你知道怎么发帖吗 这是我发送数据的代码。除此之外,我还需要将图像与此一起发送 postString = [NSString stringWithFormat:@"u=TuNmae&p=pass&o=onr&j=123&a=321&d=8765&t=123&at=need&image="]; NSMutableURL

我想用POST方法上传图片。我可以分别上传图片。但是我想把它们和我需要发送的其他数据一起发布到服务器上。谁能帮我一下吗?你知道怎么发帖吗

这是我发送数据的代码。除此之外,我还需要将图像与此一起发送

postString = [NSString stringWithFormat:@"u=TuNmae&p=pass&o=onr&j=123&a=321&d=8765&t=123&at=need&image="];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url1];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

    [req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];

    webData = [[NSMutableData data] retain];
    [webData appendData:returnData];
    NSLog(@"attempt%@",webData);
    NSString *webResult;
    webResult = [[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding];

    webResult = [webResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSLog(@"str %@",webResult);

我需要将图像插入postString。最后一个参数

必须将enctype表单atribute设置为“多部分/表单数据”

form enctype=“多部分/表单数据”

然后,您可以上传文件输入和其他文本或隐藏输入在同一个文件中 表格和相同的提交

在您的api中执行此操作


NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];


    //file data
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"ImageFile.png"];
    NSData *imageData = [[NSData alloc] initWithContentsOfFile:fullPathToFile];


Rakendu尝试以下方式,你可以通过这个发送

NSString *url = @"www.google.com";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

[request setHTTPMethod: @"POST"];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY];  
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];  

NSMutableData *body = [NSMutableData data];

[self addToDataField:body field:@"firstname" value:txtFirstName.text];
if (updatedImage == TRUE)
        {

    [body appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:UIImagePNGRepresentation(image)];  
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];  

    updatedImage = FALSE;
    }
    NSLog(@"%@",body);

NSString *temp =  [NSString stringWithFormat:@"%i", [body length]];
[request setHTTPBody:body];
[request setHTTPMethod: @"POST"];
[request addValue:temp forHTTPHeaderField:@"Content-Length"];

NSLog(@"%@",request);

NSError *error = nil;
NSURLResponse *response;
urlData = [[[NSMutableData data] retain] autorelease];
urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

希望这篇文章能对你有所帮助。

在同一篇文章中,我还想上传一张图片和其他数据,关于上传图片的大多数信息只是为了在一个服务器连接中单独上传一张图片,下面是我如何解决这个问题的

这是PHP

<?php
/* As you can see there are more values not only the image */
$variableOne    = $_POST['variableOne'];
$variableTwo    = $_POST['variableTwo'];
$variableThree  = $_POST['variableThree'];
$variableFour   = $_POST['variableFour'];
$variableFive   = $_POST['variableFive'];
$variableSix    = $_POST['variableSix'];
$variableSeven  = $_POST['variableSeven'];
$variableEight  = $_POST['variableEight'];
$variableNine   = $_POST['variableNine'];
$variableTen    = $_POST['variableTen'];

/* Our image */
$image = $_REQUEST['image'];

/* This is for trying to get a unique name for the image file, since maybe you want to store large amount of images */
$currentDate = date("Y-m-d");
$name  = "" . $currentDate . microtime() . rand(0, 999) . rand(0, 999) . rand(0, 999) . ".jpg";

/* 
 * Here comes the image stuff
 */
if (file_exists($name)) {
    echo "File already exists";
} else {
        /* Decoding image */
        $binary = base64_decode($image);

        /* Opening image */
        $file = fopen($name, 'wb');

        /* Writing to server */
        fwrite($file, $binary);

        /* Closing image file */
        fclose($file);

        echo "Added";
    }   
}   
?>
这是服务器连接代码

NSURL *sendURL = [NSURL URLWithString:@"http://yourdomainname/imagefolder/phpscript.php"];

NSMutableURLRequest *sendRequest = [NSMutableURLRequest requestWithURL:sendURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

[sendRequest setHTTPMethod:@"POST"];

[sendRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSData *imageData = UIImageJPEGRepresentation(yourImage, 1.0);

NSString *encodedString = [[self base64forData:imageData] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSString *dataToSend = [[NSString alloc] initWithFormat:@"variableOne=%@&variableTwo=%@&variableThree=%@&variableFour=%@&variableFive=%@&variableSix=%@&variableSeven=%@&variableEight=%@&variableNine=%@&variableTen=%@&image=%@", valueOne, valueTwo, valueThree, valueFour, valueFive, valueSix, valueSeven, valueEight, valueNine, valueTen, encodedString];

[sendRequest setHTTPBody:[dataToSend dataUsingEncoding:NSUTF8StringEncoding]];

serverConnection = [[NSURLConnection alloc] initWithRequest:sendRequest delegate:self];

[serverConnection start];
设置服务器连接的委托方法

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    returnData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [returnData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == serverConnection) {
        NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(@"Response: %@", responseString);

        if ([responseString isEqualToString:@"Added"]) {

            /* Make something on success */

        } else {

            /* Make something else if not completed with success */

        }
    }
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    /* Make something on failure */
}

你能提供更多的信息吗?你说的是本地应用程序吗?UIWebView/Web应用程序?不,我没有使用UIWebView。我有一些细节(字符串)随着它,我需要发送图像太多。请help@rakendu我也有同样的问题,如果你得到解决,请让我知道我可以知道这是为iPhone。因为我没有在任何地方使用标签
NSURL *sendURL = [NSURL URLWithString:@"http://yourdomainname/imagefolder/phpscript.php"];

NSMutableURLRequest *sendRequest = [NSMutableURLRequest requestWithURL:sendURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

[sendRequest setHTTPMethod:@"POST"];

[sendRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSData *imageData = UIImageJPEGRepresentation(yourImage, 1.0);

NSString *encodedString = [[self base64forData:imageData] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSString *dataToSend = [[NSString alloc] initWithFormat:@"variableOne=%@&variableTwo=%@&variableThree=%@&variableFour=%@&variableFive=%@&variableSix=%@&variableSeven=%@&variableEight=%@&variableNine=%@&variableTen=%@&image=%@", valueOne, valueTwo, valueThree, valueFour, valueFive, valueSix, valueSeven, valueEight, valueNine, valueTen, encodedString];

[sendRequest setHTTPBody:[dataToSend dataUsingEncoding:NSUTF8StringEncoding]];

serverConnection = [[NSURLConnection alloc] initWithRequest:sendRequest delegate:self];

[serverConnection start];
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    returnData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [returnData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == serverConnection) {
        NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(@"Response: %@", responseString);

        if ([responseString isEqualToString:@"Added"]) {

            /* Make something on success */

        } else {

            /* Make something else if not completed with success */

        }
    }
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    /* Make something on failure */
}