2013-12-16 137 views
0

我使用post方法上傳圖像,但每次發現以下錯誤: 操作無法完成。 (NSURLErrorDomain錯誤-1012)。上傳文件時出錯

請建議我用,我可以解決這個問題:(

NSString *urlString = [NSString stringWithFormat:@"%@%@",Base_URL,URL_Upload]; 
    NSURL* requestURL = [NSURL URLWithString:urlString]; 
    NSString *filename = imageName; 

    //creating request 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setHTTPMethod:@"POST"]; 
    // set URL 
    [request setURL:requestURL]; 

    // set Content-Type in HTTP header 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    // post body 
    NSMutableData *postbody = [NSMutableData data]; 
    NSData* imageData = UIImageJPEGRepresentation(image, 1); 

    if (imageData) { 
     [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     NSString* fileData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", filename]; 
     NSLog(@"fileData %@",fileData); 
     [postbody appendData:[fileData dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postbody appendData:imageData]; 
     [postbody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    [postbody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // setting the body of the post to the reqeust 
    [request setHTTPBody:postbody]; 

    // set the content-length 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postbody length]]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [_connection start]; 
    if (_connection) { 
     _reposnseData = [NSMutableData data]; 
    } 
+0

嘗試使用[AFNetworking]的方式(https://github.com/AFNetworking/AFNetworking) –

回答

0
NSString *str=[NSString stringWithFormat:@"%@registration.php",appdel.baseUrl]; 
    NSString *urlString = [NSString stringWithFormat:@"%@",str]; 

    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"]; 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imgData]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// parameter username 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[userName.text dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


// parameter token 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[eMail.text dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


// parameter method 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"pass\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[passWord.text dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


//parameter method 
NSLog(@"%@",countryCode); 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"country\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[countryCode dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// close form 
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil]; 
0
NSString *urlString = [NSString stringWithFormat:@"http://********/web-services/register_user.php?firstname=%@&lastname=%@&email=%@&password=%@&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password]; 
// urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 

UIImage *image=details.pic; 
NSData *imageData =UIImageJPEGRepresentation(image, 0.1); 
double my_time = [[NSDate date] timeIntervalSince1970]; 
NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)]; 
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, @".jpg\"\r\n\""]; 
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]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSDictionary *responseDictionary1; 
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil]; 
+0

當我們從iOS上傳時,服務器端代碼應該是不同的 –