2011-04-19 31 views
6

iPhoneのアプリからFTPでファイルをアップロードする際に問題が発生しています。httpを使用して複数のファイルをアップロードする

HTTP POSTリクエストは、必要なすべてのパラメータを使用してWebサービスに送信しています。私は2つのファイルを送信しています.1つは画像、もう1つは音声です。イメージはアップロードされていますが、オーディオはアップロードされていません。 ウェブサービスをトレースすると、画像フィールドのみがアップロードされたファイルとして表示されます。それは、オーディオもそこにあることを示していない。

私のコードは次のとおりです。

NSString *urlString = [NSString stringWithFormat:ADD_LEAD_API_URL]; 

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 



// change type to POST (default is GET) 
[postRequest setHTTPMethod:@"POST"]; 


// just some random text that will never occur in the body 
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo"; 

// header value 
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 

// set header 
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"]; 

// create data 
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:[appDelegate.objCurrentUser.userId dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];  


// message part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"firstName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[firstName dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

/*** My rest of string parameters are successfully added to request ***/ 


// media part 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageUrl\"; filename=\"dummy.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    // get the image data from main bundle directly into NSData object 

UIImage *img= leadImage; 
NSData *imageData= UIImageJPEGRepresentation(img,90); 

[postBody appendData:imageData]; 

// Image is being uploaded 

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"recordSoundUrl\"; filename=\"%@\"\r\n", self.recordingPath] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 


NSData *soundData = [NSData dataWithContentsOfFile:[appDelegate.documentDir stringByAppendingPathComponent:self.recordingPath]]; 
[postBody appendData:soundData]; 
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// final boundary 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// add body to post 
[postRequest setHTTPBody:postBody]; 

// Asynch request 
NSURLConnection *conn = [NSURLConnection connectionWithRequest:postRequest delegate:self]; 

私は画像をアップロードしていない場合、それはオーディオを取ります。だから、私はその要求で一つのファイルしか送ることができません。 どこに間違っていても私を助けて教えてください。

ありがとうございます。

+0

こんにちはカピル、私は同じ問題に直面しています。これで私を助けてくれますか? –

+0

何をやっていますか?私がやったやり方で複数のファイルをアップロードしようとしていますか? "[postBody appendData:imageData];を置き換えた場合と比べて、画像データを追加した後であれば、「postBody appendData:[[NSString stringWithFormat:@" - %@ \ r \ n "、stringBoundary] dataUsingEncoding:NSUTF8StringEncoding] ]; '' [postBody appendData:[[NSString stringWithFormat:@ "] \ r \ n - %@ \ r \ n"、stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]; '...これが役に立ったら –

+0

私に知らせてくださいこれは動作しません..私はいつも助けに来ています –

答えて

2

ASIHTTPRequestASINetworkQueueを使用して複数のファイルを送信しましたか?

更新日:以下のコメントごとに、ASIHTTPRequestは更新されなくなりました。このフレームワークには注意が必要です。その他のオプションはMKNetworkKitまたはAFNetworkingです。

+0

はいこれも良い選択ですが、私は自分のコード全体を変更しているわけではありません。画像データを追加した後、同じコードで '' \ r \ n "'を追加することで完了しました。いずれにせよありがとうございます –

+1

ASIHTTPRequestは、これが投稿された時点から現在に至るまで非推奨になっています。これを読んだ人は、適切な注意を払ってください。 –

+1

ありがとう@StevenFisher:更新私の答え –

0

境界の設定に問題がある可能性があります。

使用この境界として

NSString boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 

とではなく、それはかなり単純です。この

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
+1

これはほぼ1歳のポスト。あなた自身の質問にコメントが表示された場合、私はすでに自分の問題をどのように解決したのか、あなたの答えと同じです。私はすでにあなたが言ったのと同じ方法でこの問題を解決しました(私のコメントを見ることができます)。とにかく私を助けようとしてくれてありがとう.. –

+1

あなたは最も正しい答えを受け入れるべきです。あなたは受け入れられた答えを動かすことができるはずです。 –

7

のこの

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

を使用しています。ちょうどマルチパートボディを形成します。

[NSURLConnection sendAsynchronousRequest:request 
            queue:queueOfYourChoice 
         completionHandler:^(NSURLResponse *response, NSData *rdata, NSError *error) 

するFileUploadクラスはかなり簡単です::

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:54.0]; 

NSMutableData* body = [NSMutableData data]; 

NSString* boundary = [NSString randomStringWithLength:64]; 
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; 
NSData *boundaryData = [[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]; 

[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 
    [body appendData:boundaryData]; 
    // I use special simple class to distinguish file params 
    if([obj isKindOfClass:[FileUpload class]]) { 
     // File upload 
     [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", key, [obj localName]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData: [obj loadData]]; // It just return NSData with loaded file in it 
     [body appendData: [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
    else { 
     // Regular param 
     [body appendData: [[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n", key, obj] dataUsingEncoding:NSUTF8StringEncoding]] 
    } 
}]; 
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:body]; 

は今、あなたはどのような方法でそれを派遣するだけでできます:あなたは「のparams」辞書にアップロードするファイルや定期のparamsを持っていると仮定し、それから構築url/filenameは、この名前をメソッドとして提供し、NSData *にバイナリコンテンツをロードします。明確にするためにそれを列挙していませんが、必要に応じて追加します。

+0

ここにparamsは何ですか? –

+0

あなたのリクエストのパラメータを持つ辞書です。 – sergeych

関連する問題