【问题标题】:updating text from a label.text to the server and saving it in a text file将文本从 label.text 更新到服务器并将其保存在文本文件中
【发布时间】:2025-11-22 16:50:01
【问题描述】:

我正在尝试在服务器上的文本文件中将 label.text 上传到服务器上,这是我的代码。它没有显示任何错误,但在服务器上创建了文件,但其中没有文本。请帮忙。

NSString *post =[[NSString alloc] initWithFormat:@"VIN: %@",vincode.text];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
                              allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData
                                                                  length]];
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]
                                        autorelease];
        [request setURL:[NSURL URLWithString:@"Server Detail Here/upload_vin.php"]]; 
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded"
       forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                                returningResponse:nil error:nil];
        NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
        NSLog(@"VIN is %@",vincode.text);
        NSString *outputdata = [[NSString alloc] initWithData:urlData
                                                     encoding:NSASCIIStringEncoding];
        NSLog(@"Outputdata is %@",outputdata);
        NSURLResponse *response;
        NSError *error;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if(error==nil)
            NSLog(@"Error is nil");
        else
            NSLog(@"Error is not nil");
        NSLog(@"success!");
PHP file::::::::::::::
    <?php
    $file = "VinCodes.txt";
    #$submitted_vincode = $_GET['VIN'] . " \n";
    $submitted_vincode = @$_POST['VIN'] . " \n";
    if(!empty($submitted_vincode)){
    $Handle = fopen($file, 'a');
        fwrite($Handle, $submitted_vincode); 
        print "Vin Code stored in file."; 
        fclose($Handle);
    }else{
        print "Vin code is missing. Try again";
    }
    ?>

【问题讨论】:

  • $filecontents = file_get_contents("VinCodes.txt");打印$文件内容;能否请您添加此代码并打印您的文件

标签: php objective-c xcode http-method


【解决方案1】:

代码太多了,这就是我要做的,(注意我上传异步):

NSURL *url = [NSURL URLWithString: @"http://..."];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request ...

顺便说一句,您确定要NSASCIIStringEncoding 而不是NSUTF8StringEncoding

如有疑问,我会使用Charles Web 代理(30 天免费)来查看发送的确切内容和响应。

【讨论】:

  • 感谢 zaph 帮助我,但请您告诉我确切的代码应该是什么,实际上我是 Objective c 的新手,所以如果您告诉主要的整个代码部分,这将非常有帮助。我可以用 sendAsync 来做吗?如果是,那么请您提供完整的代码吗?
  • [请求 setHTTPBody:data];没有这条线???请解释一下这里的数据是什么??(根据我的代码)
最近更新 更多