使用ASIFormDataRequest上传文件不起作用

时间:2012-06-01 09:39:24

标签: iphone file-upload upload asihttprequest

我正在使用ASIHTTPRequest框架尝试通过iPhone将文件上传到我的websever。 下面是代码。只要我不使用setFile方法,我就会从服务器返回200,所以一切都很好。一旦我实现了setFile,服务器就返回0.我希望401或类似的东西,因为我可以想象我处理身份验证问题。 我的服务器是IIS,为什么我在请求中使用NTLM方式。我错过了什么吗?

NSInteger httpStatus;
NSString *httpResponseString;
NSError *httpRequestError;

NSArray *paths = [[[NSArray alloc] init] autorelease];
paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"abiliator_basis_de_ar.xml"];


NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setShouldPresentCredentialsBeforeChallenge:NO];
[request setUsername:@"myUserName"];
[request setPassword:@"myPassword"];
[request setDomain:@"myDomainName"];
[request setFile:[NSURL URLWithString:filename] forKey:@"xml"];
[request startSynchronous];
httpRequestError = [request error];
httpResponseString = [request responseString];
httpStatus = [request responseStatusCode];
if (!httpRequestError) {
    httpStatus = [request responseStatusCode];
    NSLog(@"Class %@, Method: %@ - OK login and filetransfer successful '%i'", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
} 
else {            
    NSLog(@"Class %@, Method: %@ - Error '%i' occurred sending the http request", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
}

是文件存在,这里是ls:

的结果
rene-stegs-macbook-pro:~ renesteg$ ls -ltr '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml'
-rw-r--r--@ 1 renesteg  staff  1062 22 Mai 13:44 /Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml

这里是filename的值:

Filename string is: '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml

4 个答案:

答案 0 :(得分:1)

上传文件时你做错了。

需要这样

NSFileManager *fileManager = [NSFileManager defaultManager];

    request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
    [request setRequestMethod:@"POST"];
    [request setTimeOutSeconds:120];

    [request setPostFormat:ASIMultipartFormDataPostFormat];
    [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];  
    if ([fileManager fileExistsAtPath:filePath] == YES) {

        [request setFile:filePath withFileName:@"test.xml" andContentType:@"xml" forKey:@"FieldName"];
    }

此处需要为fromdata请求设置文件路径。

希望这对你有用。

答案 1 :(得分:0)

你应该尝试使用setData:forKey:并发送xml文件的数据,例如下面的例子。

.....
NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setShouldPresentCredentialsBeforeChallenge:NO];
[request setUsername:@"myUserName"];
[request setPassword:@"myPassword"];
[request setDomain:@"myDomainName"];
[request setData:[NSData dataWithContentsOfFile:fileName] forKey:@"xml"];
[request startSynchronous];
.....

此外,您可能应该将此asynchronous请求。

答案 2 :(得分:0)

我认为错误的发生是因为你应该使用:

[request setFile:[NSURL fileURLWithPath:filename] forKey:@"xml"];

答案 3 :(得分:0)

好的,问题出在Neels评论之间:缺少PHP。 这当然需要在URL中调用,因此构建URL的正确方法是:

NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/upload.php"]; 

这样我就可以映射我的iPhone httpRequest代码和PHP。

这是必需的PHP代码:

     <?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['xml']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['xml']['tmp_name'], $target)) 
 {
    echo "The file ". basename( $_FILES['xml']['name']). " has been uploaded";
 } 
 else {
    echo "Error uploading". basename( $_FILES['xml']['name']). " occured";
 }
 ?>