-1

这是我使用 php 在 Mysql 中上传图像的 xcode 代码。NSString *urlString = @"http://www.mysullurpeta.com/iphone/upload.php";

    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"world1.jpeg"],90);
    NSLog(@"imageView in data %@/n>>>>%d kb",imageData,imageData.length);


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

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
 NSLog(@"post data is %@", contentType);
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    [request setHTTPBody:imageData];
 NSLog(@"post data is %@",request);

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        NSLog(@"Data ---- %@", str);

------这是我的php脚本

<?php
echo "file from client: " . $_FILES["filename"]["name"]; 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

我正在尝试将图像移动到“上传/”文件中。它显示错误无效文件。我在哪里做错了,我不明白。请...帮助我...谢谢

4

2 回答 2

1

使用此功能,它会在服务器中的文件夹上而不是在数据库中上传 uiimage

+(NSString *)uploadImage:(UIImage *)img :(NSString *)path :(NSString *)name {

NSLog(@"-----------------------image uploadImage uploadImage uploadImage -------------------------");

 /*
 turning the image into a NSData object
 getting the image back out of the UIImageView
 setting the quality to 90
 */
NSData *imageData = UIImageJPEGRepresentation(img, 90);

// setting up the URL to post to

NSString *urlString=WSURL;

// setting up the request object now
NSMutableURLRequest *_request = [[[NSMutableURLRequest alloc] init] autorelease];
[_request setURL:[NSURL URLWithString:urlString]];
[_request setHTTPMethod:@"POST"];

 /*
 add some header info now
 we always need a boundary when we post a file
 also we need to set the content type
 You might want to generate a random boundary.. this is just the same
 as my output from wireshark on a valid html post
 */

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

 /*
 now lets create the body of the post
 */



NSString *name1= [NSString stringWithFormat:@"%@%@",path,name];
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",name1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
//[body appendData:[NSData i:@"path=%@",path]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[_request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:_request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

return(returnString);

}

于 2012-06-04T10:51:31.547 回答
0

使用以下我已经使用此代码成功上传了mysql数据库中的记录它对我来说很好

iPhone 代码

  NSString *post =[[NSString alloc] initWithFormat:@"ProviderNPI=%@&PatientID=%@&FileURL=%@&FileTYPE=%@&DataSynID=%@&AppointmentListingsID=%@",providerNPI,patientID,FILEURL,FILETYPE,dataSynID,appointmentListingID];



    NSURL *url=[NSURL URLWithString:@"http://www.yourserver.com/Sync.php?"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);

用于插入数据库的 PHP 代码

 <?php

 $con = mysql_connect("emriphone.db.6420177.hostedresource.com","emriphone","Light12-");

 if (!$con)


   {

 die('Could not connect: ' . mysql_error());

  }

mysql_select_db("emriphone", $con);

$providernpi=$_POST['ProviderNPI'];
$patientid=$_POST['PatientID'];
$fileurl=$_POST['FileURL'];
$filetype=$_POST['FileTYPE'];
$datasynid=$_POST['DataSynID'];
$appointmentlistingsid=$_POST['AppointmentListingsID'];



  $query=("INSERT INTO AppointmentDataSync   (ProviderNPI,PatientID,FileURL,FileType,DataSyncID,AppointmentListingsID)
VALUES ('$providernpi', '$patientid','$fileurl','$filetype','$datasynid','$appointmentlistingsid')");



mysql_query($query,$con);
printf("Records inserted: %d\n", mysql_affected_rows());


 echo($appointmentlistingsid)


  ?>
于 2012-06-04T11:18:30.387 回答