2013-06-24 48 views
2

我嘗試了十幾件事。但我沒有收到結果。我想將音頻文件wav/mp3轉換爲字節數組,或者將其發送到php服務器。以下是我嘗試過的,已經列在SO中的幾件事情。請幫忙。如何通過NSURLConnection iPhone發佈音頻文件?

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的開發者說,他沒有收到任何數據任何責任。他迴應他收到的任何東西,但我找到一個空字符串作爲迴應。任何人都可以請建議做什麼?

+0

可能是這將幫助你什麼http://stackoverflow.com/questions/16195460/how-to-get-nsdata-of-mp3-using-mpmediapickercontroller – Rajneesh071

+1

謝謝Rajneesh埠我正在接收NSData值。我無法將其上傳到服務器。 – ScarletWitch

+0

您必須將NSData轉換爲base64內碼,並表示您的服務器端開發人員使用base64解碼。 – Rajneesh071

回答

-1

我這是怎麼通過ASIHTTPRequest

NSURL *url = [NSURL URLWithString:@"the url comes here"]; 
ASIFormDataRequest *formRequest = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease]; 

NSData *imageData = UIImageJPEGRepresentation([self scaleAndRotateImage:self.m_ImageViewProfile.image], 90); 

NSLog(@"Size : %d", imageData.length); 

[formRequest setTimeOutSeconds:600]; 
[formRequest setPostValue:@"If there is any"] forKey:@"the key"]; 
[formRequest setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"filename"]; 

//[formRequest setUploadProgressDelegate:progressView]; 

[formRequest setCompletionBlock:^{ 
    NSString *responseString = [formRequest responseString]; 
    NSLog(@"Response: %@", responseString); 
}]; 
[formRequest setFailedBlock:^{ 
    NSError *error = [formRequest error]; 
    NSLog(@"Error: %@", error.localizedDescription); 
}]; 

[formRequest startSynchronous]; 

NSData *audioData = Here comes your NSData; 
NSString *urlString = @"The url of php file"; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = @"---------------------------14737809831466499882746641449"; 
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:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".caf\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"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]]; 
[request setHTTPBody:body]; 

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

有另一種方式看看是不是你想要

+0

這是發佈多個數據流時很有用。當只發佈一個簡單的文件時,可能會更容易忽略多部分頭文件(取決於它在PHP端的工作方式,因爲我陷入了Python/Django世界:)我不知道) – Krumelur

+0

感謝@Krumelur for the信息 – channi

相關問題