Skip to content

Instantly share code, notes, and snippets.

@roblav96
Last active September 7, 2016 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblav96/7202d62c8f707cee56973ddd5bab579c to your computer and use it in GitHub Desktop.
Save roblav96/7202d62c8f707cee56973ddd5bab579c to your computer and use it in GitHub Desktop.
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
declare class AFHTTPRequestSerializer extends NSObject implements AFURLRequestSerialization {
multipartFormRequestWithMethodURLStringParametersConstructingBodyWithBlockError(method: string, URLString: string, parameters: NSDictionary, block: (p1: AFMultipartFormData) => void): NSMutableURLRequest;
}
interface AFMultipartFormData {
appendPartWithFileURLNameFileNameMimeTypeError(fileURL: NSURL, name: string, fileName: string, mimeType: string): boolean;
}
declare var AFMultipartFormData: {
prototype: AFMultipartFormData;
};
let request: NSMutableURLRequest = AFHTTPRequestSerializer.serializer().multipartFormRequestWithMethodURLStringParametersConstructingBodyWithBlockError(
'POST',
'http://192.168.0.3:5338/upload',
null,
function block(formData: AFMultipartFormData) {
global.tnsconsole.dump('formData', formData)
global.tnsconsole.dump('formData.description', formData.description)
global.tnsconsole.error('formData.appendPartWithFileURLNameFileNameMimeTypeError', formData.appendPartWithFileURLNameFileNameMimeTypeError)
}
)
@roblav96
Copy link
Author

roblav96 commented Sep 7, 2016

it says formData.appendPartWithFileURLNameFileNameMimeTypeError is undefined 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment