使用Rails Server跟踪iOS上视频上传的进度



我是Ruby on Rails的新手,现在我正在构建一个用于上传视频到我的Rails服务器的API。我已经建立了我的上传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");
        }
   }

我该怎么做,有什么建议吗?

您可以使用下面的委托获得发送数据和总数据,

- (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:

相关内容

最新更新