2012-04-21 166 views
2

我試圖從我的iPhone應用上傳照片到我的webserver,通過使用許多不同的.php上傳腳本,但我迄今沒有取得成功!上傳照片從iPhone到網絡服務器

請檢查我的iPhone源代碼:

- (IBAction)upload:(id)sender { 
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90); 
    NSString *urlString = @"http://wasnaga.nestons.org/420/upload.php"; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

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

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString =[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    NSLog(returnString); 
} 

有任何錯誤?你能幫助我.php文件嗎?

+0

只看這個線程的答案http://stackoverflow.com/questions/9746052/stumped-nsurlconnection-php-key-value-format-issue/9747272#9747272 – 2012-04-21 17:14:03

+0

沒有它仍然無法正常工作我也嘗試更改代碼,但沒有任何 – 2012-04-22 12:14:45

回答

0

看起來你在第一appendData行缺少一個反斜槓您有:

@"\r\n--%@r\n" 

它應該是:

@"\r\n--%@\r\n" 
相關問題