我不知道問題出在我的PHP代碼還是在objective-c方面。我不明白這一點,所以將不勝感激任何幫助獲取文件從我創建到MySQL數據庫的iPad應用程序上傳。現在,只有文件的描述被髮布到數據庫中,但blob /文件不是。任何幫助將非常感激!爲什麼我的文件沒有從iPad上傳到MySQL DB?
這是我的PHP代碼:
<?php
$username = "person";
$password = "xxxxxxx";
$database = "database";
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");
$file = $_FILES['file'];
$name = $file['tmp_name'];
$testpage = file_get_contents($name);
$testpage = mysql_real_escape_string($testpage);
mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");
mysql_close();
?>
這是我的目標c代碼,包裝成一個HTTP分組
NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[post_dict setObject:@"test_value" forKey:@"test_key"];
[post_dict setObject:[NSURL fileURLWithPath:[pdfUrl absoluteString]] forKey:@"file"];
NSData* regData = [self generateFormData:post_dict];
[post_dict release];
NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://myserver/upload.php"]];
[post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"];
[post setHTTPMethod: @"POST"];
[post setHTTPBody:regData];
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
謝謝, 羅西
謝謝,我給了一個鏡頭 - 看到我的編輯,但仍然沒有骰子...我得到的是數據庫中的文件的名稱。 – Rossi 2012-04-02 21:23:27
我使用他的generateFromData方法,並按照您的建議修改了我的代碼。上面顯示的編輯。它仍然不起作用,但我真的很感謝你的幫助!你碰巧看到其他可能錯誤的東西嗎? – Rossi 2012-04-02 22:01:43
你嚴重地是我的英雄。認真對待。感謝您的幫助!我真的很感激它!它的工作原理真的很棒。 – Rossi 2012-04-04 05:05:45