2012-04-18 7 views
1

私は画像を受信するashxスクリプトを使って、iPhoneから私のサーバに画像をアップロードしています。私が抱えている問題は、context.Request.Filesが空であることを試してみたすべてに関係なくです。iPhoneから.net ashxハンドラへの画像のアップロード

は、ここに私のXcodeのコード

NSData *imageData = [[appDelegate currentProjectData] objectForKey:@"frontImage"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.****.com/iPhoneJpgUploadHandler.ashx"]]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = @"- - - - - - - - - - Boundary String - - - - - - - - - -"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphoneimage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 
[request addValue:[NSString stringWithFormat:@"%d", [imageData length]] forHTTPHeaderField:@"Content-Length"]; 

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

されており、ここでは、スクリプトを実行して返しているので、XcodeでのNSLogが「失敗」書き出し私のアップロードスクリプト

public void ProcessRequest (HttpContext context) { 
    string uploadDir = "C:\\"; 

    if(context.Request.Files.Count > 0) { 
     HttpPostedFile file = context.Request.Files[0]; 
     file.SaveAs(Path.Combine(uploadDir, file.FileName)); 
     context.Response.Write("success"); 
    } else { 
     context.Response.Write("fail"); 
    } 
} 

内のコードです。私は何の誤りもありません。私が間違っていることは何か考えていますか?

答えて

1

私はそれを働かせました。私が変更されたすべてのは、それが唯一のポストデータの残りの部分には存在しなかった文字列になると言いましたすべての私が読んだ本

NSString *boundary = @"xxxxBoundaryStringxxxx"; 

NSString *boundary = @"- - - - - - - - - - Boundary String - - - - - - - - - -"; 

から境界文字列です。私の最初の弦はどうなっているのか分かりませんが、私が気付いていない他の規定があるかもしれません。いずれにせよ、それは今働く。

関連する問題