2017-01-25 49 views
0

我正在從文檔目錄上傳pdf。我已經推薦 this link將UIview轉換爲PDF,並且 this link將pdf上傳到服務器,但結果是pdf文件爲零字節。IOS從文檔目錄上傳pdf文件到服務器使用post和PHP

這是我的代碼。 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";} 
?> 

你能幫助我嗎?

回答

0

儘管您的代碼存在一些問題,但看起來問題在於您上傳了空白文件。您的方法createPDFfromUIView: saveToDocumentsWithFileName:將文件保存到文檔目錄並將路徑返回到文件。但是,當您使用文件內容填充NSData對象時,實際上並未使用該方法返回的路徑。更改代碼以捕獲返回的路徑字符串:

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

非常感謝您的幫助。更新代碼後,它已成功運行。謝謝。 – user3472143

相關問題