1

I currently need to upload large files from an iDevice to a server API. For this I'm attempting to use the ASIHTTPRequest library as it automatically supports uploading large files in queued chunks (before this I simply created an NSData instance with the bytes for the entire file at once and attached this to the POST message, but this causes the application to crash on larger files due to an excessive amount of RAM usage).

The problem is that when you need to upload files and add a file to the HTTP Post message, this is the syntax you need to use:

[theUploadRequest setFile:@"" forKey:@"videoupload"];

setFile requires a file path in a string format. The problem I'm currently having is that it does not seem like you are allowed to simply take the file path from a file which is not in your applications sandbox? Since I need to upload a file which is not in my application, but outside of it in the standard cameraroll.

I tried to make this quick test to see if I could create an NSData object and fill it with data from a file in the cameraroll, providing a path to it like this:

NSData *testData = [NSData dataWithContentsOfURL:theContent.defaultRepresentation.url];
NSLog(@"THE SIZE OF THE TEST DATA: %i", testData.length);

Note that "theContent" is an instance of an ALAsset, and is a file retrieved from the cameraroll. The result of this is simply a length of 0, which I suppose means you can't simply do that.

Is there any way around this? Or would I have to somehow import the video file into the application's sandbox?

CodingBeagle
  • 1,888
  • 2
  • 24
  • 52

2 Answers2

2

So if I understand correctly, you want to upload stuff straight from the camera roll? Based on the docs I'd say the important piece of code you need is:

NSString *filePath = [[info objectForKey:
                UIImagePickerControllerMediaURL] path];

The info dictionary is passed to your media picker's delegate:

- (void) imagePickerController: (UIImagePickerController *) picker
        didFinishPickingMediaWithInfo: (NSDictionary *) info;

You should then be able to use that filePath in your call to setFile.

brindy
  • 4,585
  • 2
  • 24
  • 27
  • Yes correct :) I want to upload it straight off the camera roll if it's possible. Unfortunately I think I will get a bit of a problem then, as I am unfortunately not using the media picker :( I have a custom media picker which iterates through video content from the camera roll using [AssetLibrary enumerateGroupsWithTypes], which only iterates and returns ALAsset objects, so I'm not sure if there's any way for me to get the info dictionary. – CodingBeagle Apr 12 '12 at 09:09
  • 2
    Well in that case ;) I wonder if this answers your question? http://stackoverflow.com/questions/4545982/getting-video-from-alasset – brindy Apr 12 '12 at 09:23
  • 2
    So I think the conclusion was copy it to a tmpFile in your app's sandbox and upload it from there. Not ideal. :( – brindy Apr 12 '12 at 09:24
  • ah yes, the link you provided in the comment was also the one from the below answer :) But yes this helped me out :) I will give the answer to the other post as it was quick and clear provided in the post itself, or if you edit your original answer with the link I can give you the answer points as you originally did answer before the other :) just so others might find what they are looking for if they skip comments :) But thanks a lot for your help, it have been much appriciated :) – CodingBeagle Apr 12 '12 at 10:24
1

Take a look here probably you'll find something useful.

Hope this helps.

EDIT: For something simpler you can use the bit of code posted in this answer

Implying that you need to copy the file somewhere before uploading it, especially if it is a big one.

Community
  • 1
  • 1
GreyHands
  • 1,734
  • 1
  • 18
  • 30