使用Rails服务器跟踪iOS上的视频上传进度

时间:2015-09-30 11:03:11

标签: ios ruby-on-rails ruby video carrierwave

我是Ruby on Rails的新手,现在我正在构建一个用于将视频上传到我的Rails服务器的API。我已经使用Carrierwave构建了我的上传API。这对我来说实际上很顺利。但是在iOS方面,我需要能够跟踪视频上传进度。我需要专业人员在Rails上给我一个关于如何在我的Rails API或iOS应用程序上执行此操作的建议。另外,我在上传视频时会在NSURLConnection上获得超时,这对我来说是一个问题。

Rails API代码段

    data = params[:business_asset][:video].to_s
    io = BusinessVideoString.new(Base64.decode64(data))
    io.original_filename = "foobar.mp4"
    io.content_type = "video/mp4"
    @business_asset.video = io

    if @business_asset.save
      render :json => { :video => @business.business_asset ,:message => "success"},:status => 200 
    end

在iOS端,我正在使用NSURLConnection方法

   - (void) postRequestFromUrl: (NSString *) urlString withDictionary: (NSDictionary *) post{

        NSURL *url = [NSURL URLWithString:urlString];
        NSError *error;
        NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
        NSString *  postLength = [NSString stringWithFormat:@"%d", (int)[postData length]];
        NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];

        //url where u will send data
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody: postData];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postLength length]] forHTTPHeaderField:@"Content-Length"];

        NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if(conn) {
            NSLog(@"Connection Successful");
        } else {
           NSLog(@"Connection could not be made");
        }
   }

关于我该怎么做的任何建议?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代理

获取发送数据和总数据
- (void)connection:(NSURLConnection *)connection
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

参考:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/#//apple_ref/occ/intfm/NSURLConnectionDataDelegate/connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite