2011-11-14 16 views
0

Webサービスを使用して、選択した画像を画像ビューアからサーバーにアップロードします。画像ファイルをサーバーにアップロード中にエラーが発生しました

私のWebサービスでは、ユーザーID、pwd、およびファイル名が要求されます。

しかし、私が実行すると、ファイル名に誤りがあります。

エラーは、「手順には、指定されていないファイル名が必要です」と表示されます。私はここでファイル名を渡していますが。

誰でもコードを見て、私に教えてください。何が問題なのでしょうか? - ありがとう。あなたの他の分野で

NSData *imageData; 
imageData = UIImageJPEGRepresentation(imageView.image, 90); 

//creating the url request: 
NSURL *postUrl = [NSURL URLWithString:@"myURL"]; 
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postUrl]; 

//adding header information: 
[postRequest setHTTPMethod:@"POST"]; 
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

//setting up the body: 
NSMutableData *postBody = [NSMutableData data]; 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"100"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"pwd\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"pwd"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[NSData dataWithData:imageData]]; 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[postRequest setHTTPBody:postBody]; 

// now lets make the connection to the web 
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil]; 

NSString *returnString; 
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
lblLoginStatus.text = @"Uploading done."; 

NSLog(@"%@",returnString); 
+0

私はちょうどサーバーに画像をアップロードしようと数日間を費やしましたが、本当に多くの変数があります。 iOS側ではなくサーバー側で問題になる可能性があります。 私はいくつかのことをお勧めしますか? 1.次の行をコメントアウトします。 [postBody appendData:[[NSString stringWithString:@ "Content-Type:image/jpg \ r \ n \ r \ n"] dataUsingEncoding:NSUTF8StringEncoding]; 2.コンテンツの長さを次のように追加します。 [addValue:[NSString stringWithFormat:@ "%d"、[body length]] forHTTPHeaderField:@ "Content-Length"]; – John

答えて

0

あなたは、唯一のファイル名のエントリで、あなたのフォーマット文字列で2回\r\nを持っています。多分それが問題です。

+0

ご協力いただきありがとうございます。私はこれを試みたが、それは助けにはならなかった。同じエラーが表示されます。 –

+0

私は、それが問題を引き起こす線であることはほとんど確信しています。私はあなたの書式を '; 'で分けてパラメータを分けることがわかりません。私はあなたのウェブフォームでその論理をチェックすることをお勧めします。 – Mundi

0

これを試してください:オーディオをアップロードしていますが、同じ方法で動作するはずですが。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     destinationFilePath = [[NSString alloc] initWithFormat: @"%@/audio.mp3", documentsDirectory]; 
     progress.myLabel.text = @"Uploading file..."; 
     [self.view addSubview:progress.view]; 
     NSLog(@"will upload file: %@", destinationFilePath); 
     NSString * text = captionText; 
     //this one works 
     // try this with dataWithContentOfURL 
     NSData *audioData = [[NSData alloc] initWithContentsOfURL:fileURL]; 
     NSLog(@"File URL:%@",fileURL); 

    NSString * urlString = @"urlstring" 


    // setting up the request object now 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 


    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


    NSArray *myArray = [destinationFilePath componentsSeparatedByString: @"/"]; 
    NSString *fileName = (NSString*)[myArray lastObject]; 

    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"action\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"upload" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"code\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSString *strkCode = @"code"; 

    [body appendData:[[NSString stringWithFormat:@"%@", strkCode] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:audioData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    if (![text isEqualToString:@""]) { 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"caption"] dataUsingEncoding:NSUTF8StringEncoding]]; 

     [body appendData:[text dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 
    [audioData release]; 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (connection) { 
     receivedData = [[NSMutableData data] retain]; 
    } 
    else { 
     NSLog(@"error connecting!"); 
    } 
    [connection release]; 
} 
+0

ご協力いただきありがとうございます。私はこのコードを試しました。アップロードするURL文字列があります。 あなたのコードでは、 "destinationFilePath"とは何ですか? NSArray * myArray = [destinationFilePath componentsSeparatedByString:@ "/"]; –

関連する問題