File Upload not Parsing Properly
File Upload not Parsing Properly
- Subject: File Upload not Parsing Properly
- From: Greg Hulands <email@hidden>
- Date: Tue, 31 Aug 2004 14:28:31 +1000
Hi,
I am writing a cocoa application that is an administration front end to
our web objects application. In the app we need to upload files to web
objects. I found on cocoadev.com code to construct a multipart post
using NSURL (which is attached below). Web objects is having trouble
parsing the key value pairs of the form when it receives the request. I
have nailed it down to web objects as I created a test php script that
prints the post vars in a request and it parses it fine.
When I debug it, it only gets the first key value pair of the form and
that is it. It is also putting a quote to begin with in the value of
it, for example "g when it should just be a g. This is truely baffling.
This is the content of the request without the file data so it is easer
to see the request
Content-Disposition: form-data; name="pass"
g
--curlf2RiuFIdRn36daIvycelja9wqMl
Content-Disposition: form-data; name="user"
email@hidden
--curlf2RiuFIdRn36daIvycelja9wqMl
--curlf2RiuFIdRn36daIvycelja9wqMl
--curlf2RiuFIdRn36daIvycelja9wqMl--
I tried changing the line endings to \r\n instead of just \n but it
threw exceptions before it entered the upload method.
Does anyone know why web objects would not be parsing the request and
how I can trick into parsing it?
Any help is greatly appreciated.
Regards,
Greg
- (NSData*)generateFormData:(NSDictionary*)dict
{
NSString* boundary = [NSString
stringWithString:@"curlf2RiuFIdRn36daIvycelja9wqMl"];
NSArray* keys = [dict allKeys];
NSMutableData* result = [[NSMutableData alloc] initWithCapacity:100];
int i;
for (i = 0; i < [keys count]; i++)
{
id value = [dict valueForKey: [keys objectAtIndex: i]];
[result appendData:[[NSString stringWithFormat:@"--%@\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
if ([value class] == [NSString class] || [value class] ==
[NSConstantString class])
{
[result appendData:[[NSString
stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\n\n",
[keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[result appendData:[[NSString stringWithFormat:@"%@",value]
dataUsingEncoding:NSUTF8StringEncoding]];
}
else if ([value class] == [NSURL class] && [value isFileURL])
{
[result appendData:[[NSString
stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";
filename=\"%@\"\n", [keys objectAtIndex:i], [[value path]
lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[result appendData:[[NSString stringWithString:@"Content-Type:
application/octet-stream\n\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[result appendData:[NSData dataWithContentsOfFile:[value path]]];
}
[result appendData:[[NSString stringWithString:@"\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
}
[result appendData:[[NSString stringWithFormat:@"--%@--\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
return [result autorelease];
}
- (void)uploadImage:(NSString *)file toAlbum:(SBAlbum *)album
{
NSString *urlStr = [self escapeURLString:[NSString
stringWithFormat:@"%@uploadImageIntoAlbum", AdministrationURL]];
//NSString *urlStr = @"http://localhost/~ghulands/test.php";
NSDictionary *postDict = [NSDictionary
dictionaryWithObjectsAndKeys:_username, @"user", _password, @"pass",
[album albumID], @"albumID", [NSURL fileURLWithPath:file],
@"ImageFile", nil];
NSData* regData = [self generateFormData:postDict];
NSLog(@"%@", [[[NSString alloc] initWithData:regData
encoding:NSUTF8StringEncoding] autorelease]);
NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:urlStr]];
[post addValue: @"multipart/form-data;
boundary=curlf2RiuFIdRn36daIvycelja9wqMl" forHTTPHeaderField:
@"Content-Type"];
[post setHTTPMethod: @"POST"];
[post setHTTPBody:regData];
NSURLResponse* response;
NSError* error;
NSLog(@"%@", [post allHTTPHeaderFields]);
NSLog(@"%@", [[[NSMutableString alloc] initWithData:[post HTTPBody]
encoding:NSUTF8StringEncoding] autorelease]);
NSData* result = [NSURLConnection sendSynchronousRequest:post
returningResponse:&response error:&error];
NSLog(@"%@", [[[NSString alloc] initWithData:result
encoding:NSUTF8StringEncoding] autorelease]);
}
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.