无法将PDF的NSData张贴到服务器 [英] Failing to POST NSData of PDF to server

查看:51
本文介绍了无法将PDF的NSData张贴到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad应用程序,需要在其中向服务器发送PDF文件,以及两个POST变量,用户ID和工作ID。我正在使用下面的代码来做到这一点:

I have an iPad application in which I need to send a server a PDF file, alongside two POST variables, a user ID and a job ID. I am using the code below to do this:

NSData * pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];

NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];

    NSData *validPDF = [[NSString stringWithString:@"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
    if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF])) 
    {
        NSLog (@"Not valid");
    }

    NSLog (@"%@", currentPDFFileName);
    NSLog (@"%@", [currentPDFFileName lastPathComponent]);

    //create the body
    NSMutableData *body = [NSMutableData data];

    //create the POST vars
    NSString *jobID = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"jobid\"\r\n\r\n%@", job.jobID];
    NSString *userID = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%@", delegate.userID];
    NSString *pdf = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image_file\"; filename=\"%@\"\r\n", [currentPDFFileName lastPathComponent]];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", @"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[jobID dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", @"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[userID dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", @"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:pdfData];

    NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
    [pdfRequest setValue:@"multipart/form-data; boundary=----foo" forHTTPHeaderField:@"Content-type"];
    [pdfRequest setHTTPMethod:@"POST"];
    [pdfRequest setHTTPBody:body];
    NSData *pdfDataReply;
    NSURLResponse *pdfResponse;
    NSError *pdfError;
    pdfDataReply = [NSURLConnection sendSynchronousRequest:pdfRequest returningResponse:&pdfResponse error:&pdfError];

    NSString *pdfResult = [[NSString alloc] initWithData:pdfDataReply encoding:NSASCIIStringEncoding];
    NSLog (@"%@", pdfResult);

连接另一端的人说他们没有收到PDF文件。我已经尝试了所有常见的可疑对象,例如试图找到任何零对象。

The people on the other end of the connection are saying that they are not receiving the PDF file. I have tried all the usual suspects such as trying to find any nil objects.

如果有人可以为此提供帮助,我们将不胜感激!

Would greatly appreciate if anyone could please lend a hand with this!

谢谢。
Ricky。

Thanks. Ricky.

推荐答案

原来,边界存在问题。在添加pdfData之后,我需要在末尾添加一个。谢谢您的帮助。

Turns out there was an issue with the boundaries. I needed to add one at the end, after pdfData was added. Thank you for your help.

这篇关于无法将PDF的NSData张贴到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆