2016-07-10 28 views
1

使用Django rest_framework收到了一个pdf文件。我尝试了几种方法来解析传入文件中的内容。从Django解析pdf InMemoryUploadedFile

curl -vX POST http://127.0.0.1:8000/documents/ -d @10_CSS\ (1).pdf --header "Content-Type: application/pdf" --header "Content-Disposition: attachment; filename=10_css.pdf"

def create(self, request):   
    file_ = request.FILES['file'] 

    parser = PDFParser(file_)  
    document = PDFDocument(parser) 

PDFSyntaxError: No /Root object! - Is this really a PDF?

def create(self, request):   
    file_ = request.FILES['file'] 

    parser = PDFParser(file_.read())  
    document = PDFDocument(parser) 

AttributeError: 'str' object has no attribute 'seek'

def create(self, request):   
    utf8_file = codecs.EncodedFile(request.FILES['file'], "utf-8") 
    with open('/tmp/whatever.pdf', 'wb+') as destination:   
     for chunk in request.FILES['file'].chunks():    
      destination.write(chunk)        

    file_ = open('/tmp/whatever.pdf', 'rb')      
    parser = PDFParser(file_)          
    document = PDFDocument(parser)         

PDFSyntaxError: No /Root object! - Is this really a PDF?

我试了几个PDF文件具有相同的结果。在我将它发送到我的应用程序之前,当我尝试解析一个pdf文件时,它解析得很好。 **如何从InMemoryUploadedFile解析pdf文件?

回答

0

我猜你正在使用:

parser_classes = (FileUploadParser,) 

在你的头(--header“内容处置:附件;文件名= 10_css.pdf”),这种情况下,包含在文件中。

我建议你使用MultiPartParser并且不要发送Content-Disposition头。

More informations