Uploading Audio File using HTTP POST in Objective-C
HI,
I have been trying to upload an audio file (sample.wav) to my server using the HTTP POST method by implementing the following code. There is no error connecting to the server but the file is not uploading. I have spent hours to find a solution for this but couldn't find one yet. Here is the code that I have been using to do the task.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"wav"];
// NSLog(@"filePath : %@", filePath);
NSData *postData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"postLength : %@", postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:@"http://exampleserver.com/upload.php"]];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request开发者_如何学运维 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:30.0];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"Connection Failed");
}
//[request release];
Then I am using the delegates methods of NSURLConnection to get response from my server. It does try to connect to the server but there is no response. Can anybody please help me out in this regards. And also here is the simple HTML code that I have been using to upload via simple web browser.
<form action="http://exampleserver.com/upload.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data" >
<input type="file" name="filename" />
And one last thing we must have to pass the name for input field which is "filename" here in this case. I dont know how to pass this in the objective-c. So please somebody can point me into the right direction. Any kind of help will be greatly appreciated.
regards,
Arslan
You are missing some bits in your HTTP encoding. You can't just append the file data.
your HTTP body should contain:
NSMutableData *postData = [NSMutableData data];
NSString *header = [NSString stringWithFormat:@"--%@\r\n", boundary]
[postData appendData:[header dataUsingEncoding:NSUTF8StringEncoding]];
//add your filename entry
NSString *contentDisposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"your file name"];
[postData appendData:[contentDisposition dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithContentsOfFile:@"your file path"];
NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary];
[postData appendData:[endItemBoundary dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
With that code you will be closer to what your HTTP Server expect. To complete your work, you may consider using Wireshark to diff which bytes goes over the wire with your code and with your bytes.
HTTP encoding is 'hard to get it right on your own' code. You may drop this code and use Higher level libs, such as GTMHTTFetcher or ASIHTTPRequest.
Hope this helps
精彩评论