私は十数のことを試しました。しかし、結果を受け取っていません。オーディオ ファイル wav/mp3 をバイト配列などに変換して、php サーバーに送信したいと考えています。以下は、すでにSOにリストされている、私が試したいくつかのことです。助けてください。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"r2d2" ofType:@"mp3"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:wavbundlepath]];
NSUInteger len = data.length;
int8_t *bytes = (int8_t *)[data bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:len];
[result appendString:@"["];
for (NSUInteger i = 0; i < len; i++) {
if (i) {
[result appendString:@","];
}
[result appendFormat:@"%d", bytes[i]];
}
[result appendString:@"]"];
NSString *urlString = @"http://apps2.mobiiworld.com/staging/krafttesting/";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSData* dataSend = [result dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:dataSend];
NSURLResponse *response;
NSError *err;
NSData *responseData2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData2);
次
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"r2d2" ofType:@"mp3"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:wavbundlepath]];NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *filename = @"file";
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\";filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog([NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\";filename=\"%@\"\r\n", filename]);
//[body appendData:[[NSString stringWithString:@"test"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Return : %@", returnString);
PHP 開発者は、データをまったく受け取っていないと言っています。そして、彼は受信したものをすべてエコーしましたが、応答に Null 文字列が見つかりました。誰が何をすべきか提案してもらえますか?