CodingBeagle
CodingBeagle

Reputation: 1950

ASIHTTPRequest - Uploading files from the camera roll

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?

Upvotes: 1

Views: 2177

Answers (2)

GreyHands
GreyHands

Reputation: 1794

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.

Upvotes: 1

brindy
brindy

Reputation: 4695

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.

Upvotes: 2

Related Questions