這是我的代碼的一部分...我沒有發佈它在這裏,因爲它有點長,哈哈。 我想要做的是,我嘗試抓取用戶輸入(從UITextField)並將其顯示在屏幕上(通過UITextView)。之後,我從UITextView獲取.text值,將其從NSString轉換爲NSData,然後使用php上傳腳本將NSData對象上傳到HTTP服務器。
NSString *completeName = [NSString stringWithFormat:@"%@/%@.txt",mainDelegate.gCourseCode,mainDelegate.gUID];
NSString *finalWordList = wordList.text;
NSData *data = [finalWordList dataUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = @"http://cetl.no-ip.org:50080/upload.php";
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"];
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=\"%@\"\r\n",completeName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[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);
txt文件可以上傳到服務器。但它忽略了\ n字符。
非常感謝您的幫助。
作爲一般的調試技術,您應該在每次操作後檢查數據的值。如果您知道「\ n」消失的確切步驟,答案會更清晰。 – Akusete 2010-08-10 05:55:34