我目前正在通过HTTP POST将图像上传到我的服务器。一切正常使用下面的代码。
I am currently uploading images to my server via HTTP POST. Everything works fine using the code below.
NSString *UDID = md5([UIDevice currentDevice].uniqueIdentifier); NSString *filename = [NSString stringWithFormat:@"%@-%@", UDID, [NSDate date]]; NSString *urlString = @"taptation/stationary_data/index.php"; request= [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *postbody = [NSMutableData data]; [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[NSData dataWithData:imageData]]; [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postbody]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(returnString);但是,当我尝试将其转换为使用Image Shacks XML API时,任何东西。从ImageShack的方向如下。
However, when I try to convert this to work with Image Shacks XML API, it doesn't return anything. The directions from ImageShack are below.
通过POST将以下变量发送到imageshack。 us /index.php
Send the following variables via POST to imageshack. us /index.php
fileupload; (image) xml =yes; (指定返回XML) cookie; (注册码,可选)
fileupload; (the image) xml = "yes"; (specifies the return of XML) cookie; (registration code, optional)
有人知道我应该从这里开始吗?
Does anyone know where I should go from here?
推荐答案您可能需要考虑使用 ASIHTTPRequest ,因为它将为你建立一个表单数据的文章主体,减少了很多麻烦,并且可以从磁盘流式传输请求主体,因此在上传大图片时,可以节省内存。
You might want to consider using ASIHTTPRequest, as it will build a form data post body for you with a lot less hassle, and can stream the request body from disk, so you'll save memory when uploading large images.
Google快速找到这,似乎建议您发布到/upload_api.php而不是/index.php。
A quick google found this, which seems to suggest you should be posting to /upload_api.php rather than /index.php.
这样可能是一个好的开始:
Something like this would probably be a good start:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithUrl:[NSURL URLWithString:@"www.imageshack.us/upload_api.php"]] autorelease]; [request setFile:@"/path/to/file" forKey:@"fileupload"]; [request setPostValue:@"yes" forKey:@"xml"]; [request setPostValue:@"blahblah" forKey:@"cookie"]; //It looks as though you probably need these too [request setPostValue:@"me@somewhere" forKey:@"email"]; [request setPostValue:@"blah" forKey:@"key"]; [request start]; if ([request error]) { NSLog(@"%@",[request error]); } else { NSLog([request responseString]); // The xml that got sent back }警告:未经测试!
我使用了一个同步请求,因为你使用了同步请求,但你几乎肯定应该使用一个异步请求(使用ASIHTTPRequest的队列)。
I've used a synchronous request because you did, but you almost certainly should be using an asynchronous request instead (a queue with ASIHTTPRequest).
更多推荐
HTTP POST到Imageshack
发布评论