ios - 上传xml文件到服务器
<p><p>我正在尝试上传这样的文件:</p>
<pre><code>- (NSString *)uploadWithTarget:(NSString *)url andFileData:(NSData *)file andMD5Checksum:(NSString *)checksum andFileName:(NSString *)name
{
uploadFinished = false;
NSString *response = @"";
NSURLSessionConfiguration *defaultConfigObject = ;
NSURLSession *defaultSession = ];
NSURL * urll = ;
NSMutableURLRequest * urlRequest = ;
;
;
;
NSString *boundary = @"*****";
NSString *contentType = ;
;
/*
now lets create the body of the post
*/
NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
;
NSURLSessionDataTask * dataTask = ;
//;
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 %@", [ initWithData: encoding:NSUTF8StringEncoding]);
} else {
}
}];
;
return response;
}
</code></pre>
<p>但我的服务器总是响应未找到文件!这不是正确的代码吗?!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>- (void) uploadLog :(NSString *) filePath
{
NSData *data = [ initWithContentsOfFile:filePath];
NSString *urlString =;
// to use please use your real website link.
NSMutableURLRequest *request = [ init];
];
;
NSString *boundary = @"_187934598797439873422234";
NSString *contentType = ;
;
;
;
;
NSMutableData *body = ;
] dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
;
] forHTTPHeaderField:@"Content-Length"];
NSData *returnData = ;
NSString *returnString = [ initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
</code></pre>
<p>这是适合我的 php 部分。</p>
<pre><code><?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!";
}
?>
</code></pre></p>
<p style="font-size: 20px;">关于ios - 上传xml文件到服务器,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21330686/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21330686/
</a>
</p>
页:
[1]