2014-01-24 68 views
2

我想上傳一個文件是這樣的:上傳XML文件服務器

- (NSString *)uploadWithTarget:(NSString *)url andFileData:(NSData *)file andMD5Checksum:(NSString *)checksum andFileName:(NSString *)name 
{ 
uploadFinished = false; 
NSString *response = @""; 

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; 

NSURL * urll = [NSURL URLWithString:url]; 
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:urll]; 
[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest addValue:checksum forHTTPHeaderField:@"Md5Hash"]; 
[urlRequest addValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"]; 


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

/* 
now lets create the body of the post 
*/ 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/xml\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:file]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// setting the body of the post to the reqeust 
[urlRequest setHTTPBody:body]; 


NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest]; 
//[dataTask resume]; 

NSURLSessionUploadTask *uploadTask = [defaultSession 
             uploadTaskWithRequest:urlRequest 
             fromData:file 
             completionHandler:^(NSData *data, 
                  NSURLResponse *response, 
                  NSError *error) 
{ 
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; 

    if (!error && httpResp.statusCode == 200) { 
     NSLog(@"Request body %@", [[NSString alloc] initWithData:[urlRequest HTTPBody] encoding:NSUTF8StringEncoding]); 

    } else { 

    } 
}]; 


[uploadTask resume]; 

return response; 
} 

但我的服務器總是響應文件不存在!那是不是正確的代碼?

+0

尋求服務器管理員的支持。 – trojanfoe

+0

但與我的代碼是一切正常?! – davidOhara

+0

不知道 - 沒看。如果你無法從服務器管理員獲得支持(例如,他不知道你),那麼你需要使用這樣的web調試代理來捕獲HTTP請求和響應:http://www.charlesproxy .com /或者像這樣:http://fiddler2.com/ – trojanfoe

回答

1
- (void) uploadLog :(NSString *) filePath 
{ 
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; 
NSString *urlString =[NSString stringWithFormat:@"http://www.yoursite.com/accept.php"]; 
// to use please use your real website link. 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = @"_187934598797439873422234"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 
[request setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14" forHTTPHeaderField:@"User-Agent"]; 
[request setValue:@"http://google.com" forHTTPHeaderField:@"Origin"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"Content-Length %d\r\n\r\n", [data length] ] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture\"; filename=\"%@.png\"\r\n", @"newfile"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"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]; 
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"]; 


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@", returnString); 
} 

這裏是php的一部分,適合我。

<?php 
    $target_path = "./uploads/"; 

    $target_path = $target_path . basename($_FILES['picture']['name']); 

    if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['picture']['name'])." has been uploaded"; 
    } else{ 
    echo "There was an error uploading the file, please try again!"; 
    } 
?> 
+0

就像一個魅力... thx! – davidOhara

+0

高興地幫助享受快樂編碼... + 1給我 –