2013-08-21 27 views
5

Tôi mới vào khung Tornado. Khi tôi đặt loại tiêu đề application/pdf, Tuy nhiên, chỉ cần MIME Type mặc định, tức là; plian/text. Ở đây mã của tôi,Làm thế nào để xử lý loại MIME trong cơn lốc xoáy?

class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
      ifile = open("requirements.txt", "r") 
      self.set_header('Content-Type', 'application/pdf; charset="utf-8"') 
      self.set_header('Content-Disposition', 'attachment; filename="test.pdf"') 
      #print(self.list_headers()) 
      self.write(ifile.read()) 

Tải xuống thành công thông qua trình duyệt web. Ở đây url http: /203.193.173.102: 8888 /. Nhưng khi tôi mở tập tin pdf nó không được mở. Bất kỳ ai giúp tôi. Cảm ơn

+0

Tại sao bạn mở 'requirement.txt' thay vì 'test.pdf'? – falsetru

+0

Cảm ơn bạn đã trả lời. Tôi không biết cách đọc tập tin pdf trong cơn lốc xoáy. – dhana

Trả lời

7

Hãy thử:

class MainHandler(tornado.web.RequestHandler): 
    def get(self): 
     with open('test.pdf', 'rb') as f: 
      self.set_header("Content-Type", 'application/pdf; charset="utf-8"') 
      self.set_header("Content-Disposition", "attachment; filename=test.pdf")     
      self.write(f.read()) 
+0

Tôi đã thử điều này. Nhưng nó cho lỗi -> UnicodeDecodeError: 'utf8' codec không thể giải mã byte 0xb5 ở vị trí 10: byte bắt đầu không hợp lệ – dhana

+0

Tôi đã chỉnh sửa câu trả lời, hãy thử lại. – alecxe

+0

Tôi gặp lỗi tương tự. – dhana

Các vấn đề liên quan