PNG和多部分HTTP请求

时间:2013-03-08 13:36:21

标签: ios multipartform-data nsmutableurlrequest

我正在尝试将文件及其缩略图从iPad发送到HTTP服务器

调用

NSLog(@"Sending  file:    %@  " , file  );
[self sendFileRemotely:file];
NSLog(@"Sending  thumb:   %@  " , thumb );
[self sendFileRemotely:thumb];

生成HTTP请求

NSString *encType     = @"multipart/form-data";
NSString *contType    = @"Content-Type: application/octet-stream";
NSString *flag        = @"true";
NSString *urlString   = [NSString stringWithFormat:@"http://%@/upload.psp?format=json" , service.hostName];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSData *FileData      = [NSData dataWithContentsOfFile:path];
NSMutableData *body   = [NSMutableData data]; 
NSString *boundary    = [NSString stringWithFormat:@"0xKhTmLbOuNdArY-1B46-EEDS-%f", [[[NSDate alloc] init] timeIntervalSince1970] ];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
                                                               /* charset=utf-8;   */

[request addValue:contentType  forHTTPHeaderField: @"Content-Type"];
[request addValue:@"gzip"      forHTTPHeaderField: @"Accept-Encoding"];

//  parameter force_flag
[body appendData:[[NSString   stringWithFormat:@"--%@\r\n", boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"force_flag\"\r\n\r\n"]                      dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[flag        dataUsingEncoding:NSUTF8StringEncoding]];                               [body appendData:[@"\r\n"     dataUsingEncoding:NSUTF8StringEncoding]];
//  parameter filename 
[body appendData:[[NSString   stringWithFormat:@"--%@\r\n", boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"file\"\r\n\r\n"]                            dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filename    dataUsingEncoding:NSUTF8StringEncoding]];                               [body appendData:[@"\r\n"     dataUsingEncoding:NSUTF8StringEncoding]];
//  parameter url
[body appendData:[[NSString   stringWithFormat:@"--%@\r\n", boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"url\"\r\n\r\n"]                             dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[url         dataUsingEncoding:NSUTF8StringEncoding]];                               [body appendData:[@"\r\n"     dataUsingEncoding:NSUTF8StringEncoding]];
//  parameter enctype
[body appendData:[[NSString   stringWithFormat:@"--%@\r\n", boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"enctype\"\r\n\r\n"]                         dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[encType     dataUsingEncoding:NSUTF8StringEncoding]];                               [body appendData:[@"\r\n"     dataUsingEncoding:NSUTF8StringEncoding]];
//  parameter content encoding
[body appendData:[[NSString   stringWithFormat:@"--%@\r\n", boundary]   dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString   stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", filename]     dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[contType    dataUsingEncoding:NSUTF8StringEncoding]];                               [body appendData:[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData      dataWithData:FileData]]; 
[body appendData:[[NSString   stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

所有缩略图都是PNG文件。 这是奇怪的部分

发送PDF文件和缩略图都工作 发送PNG文件工作和缩略图失败400错误请求
发送MOV文件工作和缩略图失败400错误请求

PDF的缩略图(PNG)如何工作,而其他缩略图失败?

非常感谢您的建议 提前致谢

0 个答案:

没有答案