【问题标题】:IOS Upload pdf file from document directory to server using post and PHPIOS使用post和PHP将pdf文件从文档目录上传到服务器
【发布时间】:2017-01-25 17:37:48
【问题描述】:

我正在从文档目录上传 pdf。我参考 this link 将 UIview 转换为 PDF, this link 将 pdf 上传到服务器,但结果是 pdf 文件零字节。

这是我的代码。 Xcode端:

-(NSMutableData *)createPDFDatafromUIView:(UIView*)aView
{
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    return pdfData;
}

-(NSString*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    NSMutableData *pdfData = [self createPDFDatafromUIView:aView];

    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
    [aView.layer renderInContext:pdfContext];
    // remove PDF rendering context
    UIGraphicsEndPDFContext();
    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
    NSLog(@"The pdf is saved in %@", documentDirectoryFilename);
    return documentDirectoryFilename;
}

-(IBAction)snapchot:(id)sender
{
    NSDate*todayDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"ddMMyyyy"];
    NSString *mytext = [NSString stringWithFormat:@"%@.pdf",[dateFormatter stringFromDate:todayDate]];


    [self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
    NSData *datadat = [[NSData alloc] initWithContentsOfFile:mytext];
    NSString *urlString = @"http://localhost/pfy/uploadtestformpdf.php";
    NSString *filename = @"filename2";
    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [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=\"%@.pdf\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/pdf\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:datadat]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", returnString);
}

PHP端:

<?php 
$uploaddir = './uploaded/';
$file = basename($_FILES['userfile']['name']); $uploadfile = $uploaddir . $file; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "Received file: $file";}
?>

你能帮我吗?

【问题讨论】:

    标签: php ios objective-c pdf


    【解决方案1】:

    尽管您的代码存在一般问题,但问题似乎在于您正在上传一个空白文件。您的方法createPDFfromUIView: saveToDocumentsWithFileName: 将您的文件保存到文档目录并返回文件的路径。但是,当您使用文件的内容填充 NSData 对象时,您实际上并没有使用该方法返回的路径。更改代码以捕获返回的路径字符串:

    NSString *path = [self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
    NSData *datadat = [[NSData alloc] initWithContentsOfFile:path];
    

    【讨论】:

    • 非常感谢您的帮助。更新代码后,成功运行。谢谢。
    猜你喜欢
    • 2013-12-29
    • 2013-12-14
    • 1970-01-01
    • 2012-11-04
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    相关资源
    最近更新 更多