[英]Upload image to server using Xcode 6 and PHP file
我試圖根據用戶的選擇將圖像發送到我的網站,每次應用程序都應該給出用戶選擇的圖像名稱。
我正在使用Xcode 6和以下代碼:
NSString *urlString = @"http://MyWebSite.com/sendImages.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image,90);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:returnString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
她是我的PHP文件:
<?
$myparam = $_FILES['userfile']; //getting image Here //getting textLabe Here
$target_path = "khadamati/";
if(move_uploaded_file($myparam['tmp_name'], $target_path . basename( $myparam['name'])))
{ echo "The file ". basename( $myparam['name']). " has been uploaded"; }
else
{ echo "There was an error uploading the file, please try again!"; }
?>
如何在此行中更改myImage名稱:
[body appendData:[@"Content-Disposition: attachment; name=\"userfile\"; filename=\"myImage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
我試圖找到任何可以幫助我的薄,但我不能,所以請在我的展位代碼中找到錯誤。
你應該試試這個,
uname,datestring(我在這里用來定義上傳的用戶和他上傳的時間)。
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@receipt%@.jpg\"\r\n", uname,datestring]] dataUsingEncoding:NSUTF8StringEncoding]];
我希望它對你有所幫助,uname和datestring都是字符串!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.