0

我正在使用 ASIHTTPRequest 框架尝试通过 iPhone 将文件上传到我的网络服务器。下面是代码。只要我不使用 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 string is: '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
4

4 回答 4

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请求设置文件路径。

希望这对你有用。

于 2012-06-01T10:16:14.320 回答
0

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

[request setFile:[NSURL fileURLWithPath:filename] forKey:@"xml"];
于 2012-06-01T11:23:36.273 回答
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请求。

于 2012-06-01T10:33:55.137 回答
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";
 }
 ?> 
于 2012-06-01T12:21:46.280 回答