0

我想将我的 pdf 文件数据与一些用户数据(如 userId 和 fileName)一起发送到服务器。

我已经尝试了 2 种解决方案,但没有在后端获得 pdf 文件。即使我的文档目录中有pdf文件,它也可以成功打开。

试过1:

-(void)uploadDock{
NSString *fileName =[NSString stringWithFormat: @"%@.pdf",@"Inspection"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSData *data=[NSData dataWithContentsOfFile:path];
    NSString *content=[data base64EncodedString];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];


if ([self.responseData retainCount]) {
    NSLog(@"Do nothing");
    [self.responseData release];
}
self.responseData=[[NSMutableData alloc]init];
NSURL *url = [NSURL URLWithString:@"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];

//self.request = [NSMutableURLRequest requestWithURL:url
//cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

self.request = [NSMutableURLRequest requestWithURL:url];
NSLog(@"Requst %@",request);
NSString *userid=[userDefaults valueForKey:@"UIDD"];
NSString *title=fileName;
NSString *docname=fileName;
NSString *size=[NSString stringWithFormat:@"%d",[data length]];
NSString *post = [NSString stringWithFormat:@"userid=%@&title=%@&docname=%@&size=%@&content=%@",userid,title,docname,size,content];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[self.request setHTTPMethod:@"POST"];

//NSString *POSTBoundary = [NSString stringWithFormat:@"0xKhTmLbOuNdArY"];
//[self.request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];

[self.request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[self.request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[self.request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:self.request delegate:self];
}

试过2:

我正在从 Document 目录中读取 pdf 文件的 NSData。///// -(void)uploadDock{

self.condition=3;

NSString *fileName =[NSString stringWithFormat: @"%@.pdf",@"Inspection"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *data=[NSData dataWithContentsOfFile:path];

NSString *content=[data base64EncodedString];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if ([self.responseData retainCount]) {
    NSLog(@"Do nothing");
}
else{
    self.responseData=[[NSMutableData alloc]init];
}

NSURL *url = [NSURL URLWithString:@"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];
//self.request = [NSMutableURLRequest requestWithURL:url
  //                                     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.request = [NSMutableURLRequest requestWithURL:url];
NSLog(@"Requst %@",request);
NSString *userid=[userDefaults valueForKey:@"UIDD"];
NSString *title=fileName;
NSString *docname=fileName;
NSString *size=[NSString stringWithFormat:@"%d",[data length]];

/*
NSString *post = [NSString stringWithFormat:@"userid=%@&title=%@&docname=%@&size=%@&content=%@",userid,title,docname,size,content];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
 [self.request setHTTPMethod:@"POST"];

//NSString *POSTBoundary = [NSString stringWithFormat:@"0xKhTmLbOuNdArY"];
//[self.request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];

[self.request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[self.request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[self.request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:self.request delegate:self];
*/

/////
NSMutableURLRequest *request11 = [[NSMutableURLRequest alloc] init] ;
[request11 setURL:url];
[request11 setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request11 addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
// Pdf File
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"content\"; filename=\"INSPECTION.pdf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[@"Content-Disposition: form-data; name=\"content\"\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",userid] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",@"TITLE_TEST1"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"docname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",docname] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"size\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",size] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[request11 setHTTPBody:body];
[NSURLConnection connectionWithRequest:request11 delegate:self];
//[NSURLConnection sendSynchronousRequest:request11 returningResponse:nil error:nil];

/////
}
4

6 回答 6

1

我使用以下功能上传图片、音频和视频,并稍作修改。在此函数urloptions中是您要与文件一起发送的查询字符串,例如userId and fileName.

没有机会上传 PDF,但希望这会对您有所帮助。

-(NSString *)uploadFile:(NSString *)urloptions: (NSString *) ext :(NSData *)imageData{

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@&%@",APP_URL,urloptions]];   
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"formFile\"; filename=\".%@\"\r\n",ext] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

//NSLog(@"%@",returnString);
return returnString ;

 //[returnString release];

}
于 2013-06-13T11:07:43.457 回答
1

虽然您可以找到正确发布数据的方法,但使用 NSURLConnection 周围的包装器(例如 STHTTPRequest https://github.com/nst/STHTTPRequest )会更容易且可维护。这是您的代码的样子:

STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];

[r setFileToUpload:@"Inspection.pdf" parameterName:@"myFile"];

[r setPOSTDictionary:@{@"userid":@"", @"title":@"", @"docName":@""}]; // your parameters here

r.completionBlock = ^(NSDictionary *headers, NSString *body) {
    // ...
};

r.errorBlock = ^(NSError *error) {
    // ...
};

[r startAsynchronous];
于 2013-06-13T11:14:42.847 回答
0

首先将您的pdf转换为二进制

 NSData theData = [NSData dataWithData:[GTMBase64 decodeString:theBinary]]; //first transfer it to NSData.

[m_oTestingWeb loadData:theData
           MIMEType:@"application/pdf"
   textEncodingName:@"UTF-8"
            baseURL:nil];                   //using the web view to show it back

然后将此数据上传到服务器。您必须在 iPhone 可以联系的 Web 服务器上创建一个 API,以便将数据 POST 到您的 Web 服务器。您可以简单地创建一个 NSURLConnection 来构建您的数据包以从 iPhone 应用程序发布数据。

在 NSURLConnection 中,您可以告诉它是一个 POST 数据包,然后将数据添加到请求的正文中。您的图像数据应转换为 UTF8 并以 nvarchar 或类似的方式存储在数据库中。

了解,这是对您必须做的事情的概述,在不了解您的网络应用程序的内部运作的情况下,我无法为您提供具体信息。

于 2013-06-13T11:12:38.097 回答
0

您可以通过以下方式使用最新的库AFNewtorkAFNetworking您可以通过以下方式使用:

NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourfile.pdf"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"MainMedia" fileName:@"MainMedia" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation start];

}

下载AFNetworking库并将其添加到您的项目中。

于 2013-06-13T11:21:16.800 回答
0

要上传任何类型的文件,您可以使用从https://github.com/pokeb/asi-http-request 获得的 ASIHttpRequest 库。

要上传文件,请使用ASIFormDataRequest该库的类,使您的工作变得轻松。

于 2013-06-13T11:08:15.510 回答
0

我可以在“尝试 2”中发现的两个实际问题

  1. 看来,您忘记了最后的分隔符 - 请参阅 Irfan DANISH 帖子。
  2. 您需要 pdf 的二进制文件 - 而不是 UTF8。

此外,不要使用同步请求 - 使用实现委托方法的异步样式。

而且,您“应该”为 pdf 部分设置 Content-Length 标头。

于 2013-06-14T20:41:06.217 回答