Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Created October 6, 2015 18:50
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 pixelrevision/f9c5ed9715abde070e5c to your computer and use it in GitHub Desktop.
Save pixelrevision/f9c5ed9715abde070e5c to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSURLRequestFileInfo : NSObject
@property NSData *data;
@property NSString *name;
@property NSString *key;
+ (NSURLRequestFileInfo *)fileInfoWithData:(NSData *)data key:(NSString *)key andName:(NSString *)name;
@end
@interface NSURLRequest (FileAdditions)
+ (NSMutableURLRequest *)fileUploadRequestWithURL:(NSURL *)url withFiles:(NSArray *)files variables:(NSDictionary *)variables;
@end
#import "NSURLRequest+FileAdditions.h"
@implementation NSURLRequestFileInfo
+ (NSURLRequestFileInfo *)fileInfoWithData:(NSData *)data key:(NSString *)key andName:(NSString *)name{
NSURLRequestFileInfo *info = [[NSURLRequestFileInfo alloc] init];
info.data = data;
info.name = name;
info.key = key;
return info;
}
@end
@implementation NSURLRequest (FileAdditions)
+ (NSMutableURLRequest *)fileUploadRequestWithURL:(NSURL *)url withFiles:(NSArray *)files variables:(NSDictionary *)variables{
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
NSMutableData *postData = [NSMutableData data];
NSString *myboundary = @"14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
for(NSURLRequestFileInfo *fileInfo in files){
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fileInfo.key, fileInfo.name] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:fileInfo.data]];
}
if(variables.count > 0){
for(NSString *key in variables){
NSString *parameterValue = [variables objectForKey:key];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@",key,parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}
}else{
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)postData.length];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postData];
return urlRequest;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment