php - 如何将字符串和视频发布到 PHP 服务器?

标签 php ios post upload nsurlconnection

我有以下问题.. 我正在尝试将视频数据和一些字符串发布到 PHP 服务器。我可以发布视频数据,但我不知道如何添加字符串。这是我正在使用的代码,它非常适合发布视频,但现在我必须在帖子中添加文本。

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"video_mp4\"; filename=\"movie.mp4\"\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];

    // Get the post header int ASCII format:
    NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    // Generate the mutable data variable:
    NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length]];
    [postData setData:postHeaderData];

    // Add the video:
    [postData appendData: uploadData];

    // Add the closing boundry:
    [postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

    // Return the post data:
    return postData;
}

-(void)post:(NSData *)fileData
{
    NSLog(@"POSTING");

    // Generate the postdata:
    NSData *postData = [self generatePostDataForData: fileData];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    // Setup the request:
    NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://pik.bg/android_test.php"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30];
    [uploadRequest setHTTPMethod:@"POST"];
    [uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
    [uploadRequest setHTTPBody:postData];

    // Execute the reqest:
    //NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
     NSData *returnData = [NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog (@"%@", returnString);
}

    // Button for sending the news to the web.
-(IBAction)sendNews:(id)sender
{
    NSData *videoData = [NSData dataWithContentsOfFile:videoString];
    [self post:videoData];
}

现在我想在“标题”和“描述”文本框中添加一些文本。如何用这种代码做到这一点?

最佳答案

使用这可能会有帮助..

NSString *strURL =@"Your URL";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:strURL]];
[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];
//Image Uploading
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"video_mp4\"; filename=\"movie.mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

NSData *videoData = @"Your video in data Format";
[body appendData:[@"Content-Type: video/mp4\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:videoData]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


//Data Sending
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Your Key That's need on server\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Your Data That's you want to send on server" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"data Return From Server %@",returnString);

我已经改变了我的答案。现在根据您的服务器需要检查和安排。

关于php - 如何将字符串和视频发布到 PHP 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538273/

相关文章:

javascript - AngularJS 中的 HTTP POST 请求返回 403

php - 当 friend 在 PHP/MySQL 应用程序中忽略某人时,我应该如何处理?

php - 模拟服务提供商 - Laravel 5.4

ios - 覆盖 UIActivityViewController 默认行为

ios - 如何将包含动态高度内容的弹出 View 居中?

c# - 将 json post 请求中的多个复杂参数传递给 asp.net WebApi

jquery - 如何使用jquery方法post发送tinymce内容

php - MySQL 不区分大小写的查询,带空格的字符串

php - 如何更新数据库中所有表的所有记录?

ios - 如何使用模型类初始化 Core Data ManagedObjectContext?