我正试图从我的应用程序中按下一个按钮,它将把设备上的sqlite文件的副本发送到我的webservice,以便在那里进行进一步的操作。我一直没有得到结果,因为它没有发送文件。
下面是我发送文件的当前代码
- (void)sendFile
{
//NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDir = [docPaths objectAtIndex:0];
// NSString *filePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
NSString *fileName = @"database.sqlite";
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
NSString *serverURL = @"http://webserver/upload.php";
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
if (!fileData) {
    NSLog(@"Error: file error");
    return;
}
if (self.urlConnection) {
    [self.urlConnection cancel];
    self.urlConnection = nil;
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL URLWithString:serverURL]];
[request setTimeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"780808070779786865757";
/* Header */
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
/* Body */
NSMutableData *postData = [NSMutableData data];
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:fileData];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
if (self.receivedData) {
    self.receivedData = nil;
}
self.receivedData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"finish requesting: %@", [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]);
self.urlConnection = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"requesting error: %@", [error localizedDescription]);
self.urlConnection = nil;
}我当前的php脚本是
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>我的输出是
 <pre>Possible file upload attack!
 Here is some more debugging info:Array
 (
 [file] => Array
    (
        [name] => onehealth.sqlite
        [type] => application/octet-stream
        [tmp_name] => /tmp/phpeg93fL
        [error] => 0
        [size] => 262144
    )
 )更多的信息,我正在运行这个模拟器,这会是一个问题吗?这还会在ipad上留下一份sqlite文件的副本吗?还是我要复印一份然后把副本寄出去?
发布于 2013-11-20 03:42:46
问题在于目标C代码或PHP代码。
简单地说,您正在创建一个包含“文件”字段的表单,以便该文件上传。因此,在代码php中,您必须使用键'file‘而不是'userfile’。
PHP代码的解决方案是:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>或者,如果不想更改php代码,可以更改ObjC代码,更改下一行:
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];为此:
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"userfile\"; filename=\"database.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];我有另一个建议:您可以使用库AFNetworking来管理服务器的请求。你已经解决了很多问题。比如上传文件。
https://stackoverflow.com/questions/20037184
复制相似问题