วิธีการทำงานของ Viewer

วงจรการร้องขอเอกสาร

Doconut renders documents as paginated images, served by an HTTP handler your Web.config registers. Understanding the lifecycle — open, token, page requests, close — explains almost every behavior you will observe, including the error messages.

ส่วนประกอบที่ทำงาน

  • Viewer — บริการสาธารณะที่คุณดึงจาก DoconutHost.Services. มันเปิดเอกสารและคืนค่าโทเค็นเซสชัน.
  • เซสชันเอกสาร — วัตถุฝั่งเซิร์ฟเวอร์ที่เก็บเอกสารที่โหลดแล้ว, อยู่ในหน่วยความจำภายใต้โทเค็นของมัน.
  • DocImage.axd — Doconut.DocImageHandler ที่ประกาศใน Web.config. มันตอบสนองต่อทุกคำขอที่วิดเจ็ตในเบราว์เซอร์ทำ (หน้า, ภาพย่อ, การค้นหา, คำอธิบายประกอบ, …) โดยอ้างอิงโทเค็นเสมอ.
  • /doconut-res/* — Doconut.DoconutResourceModule, ซึ่งให้บริการสคริปต์, สไตล์, และไอคอนของวิดเจ็ตเอง.

Viewer is stateless — by design

Viewer holds no per-request document state and does not implement IDisposable. Sessions live independently of it and are cleaned up by expiration or an explicit CloseDocument(token). System.Web has no constructor injection, so resolve it where you need it — typically in a controller action:

csharp
// POST /ViewerApi/Open?fileName=Sample.pdf
[HttpPost]
public async Task<ActionResult> Open(string fileName)
{
    var viewer = Doconut.DoconutHost.Services.GetRequiredService<Viewer>();
    var path   = Server.MapPath("~/files/" + Path.GetFileName(fileName));

    var token = await viewer.OpenDocumentAsync(path);
    return Content(token, "text/plain");
}

What happens inside OpenDocumentAsync

  1. License gate. A rejected license (tampered, blacklisted, or a build outside the license's update window) throws LicenseException immediately, with the reason as the message. Opening never silently degrades for an invalid license; a missing license only watermarks.
  2. Session creation. The viewer factory picks the format viewer for the file extension and loads the document (see กระบวนการเรนเดอร์). The session is stored in memory under a new random token with a sliding expiration of DocOptions.TimeOut minutes (default 60). Every page request resets the clock.
  3. Access grant. With UnsafeMode = false (the default), the token is bound to the ASP.NET session of the request that opened it: a secure-{token} entry is written into that session. The open must therefore run in a request with session state — see เซสชันและความปลอดภัย.
  4. The token is returned. It is the only identifier the browser needs for everything that follows.

The asynchronous overloads take a file path, a file path plus a per-format config (PdfConfig, WordConfig, …), or a Stream plus a FileInfo whose extension selects the format. For Web Forms code-behind that does not use asynchronous pages, OpenDocument offers the same three inputs synchronously (a path, a stream with a file name, or a byte array with a file name).

วิธีที่วิดเจ็ตรับหน้าต่าง

The widget calls DocImage.axd with the token in the query string. What the handler does depends on the request:

คิวรีวัตถุประสงค์
?token=…&page=Nภาพหน้าที่เรนเดอร์ (PNG)
?token=…&page=N&thumb=1ภาพย่อ
?token=…&zoom=…การเรนเดอร์หน้าที่ซูม
?token=…&search=termการค้นหาข้อความเต็ม (ต้องมีใบอนุญาต)
?token=…&bookmarksโครงสร้างเอกสารและบุ๊คมาร์ค
?token=…&copy / &showlinks / &fileFormat / &metaคัดลอกข้อความ, แสดงลิงก์, ข้อมูลรูปแบบไฟล์, เมทาดาต้า DICOM
?token=…&action=rotate/flip/closeการกระทำหน้าต่างและการปิดโดยตรง (POST)
?token=…&AnnSave=… / &AnnLoadบันทึกและโหลดคำอธิบายประกอบ

ทุกคำขอจะถูกตรวจสอบก่อน:

  • ไม่มีโทเค็น → 404 หรือแบนเนอร์เวอร์ชันเมื่อ ShowDoconutInfo = true.
  • โทเค็นไม่ได้รับมอบหมายให้กับเซสชัน ASP.NET นี้ → รูปภาพข้อผิดพลาดที่แสดงข้อความ You Are Not Authorized To View This Page. (คุณไม่ได้รับอนุญาตให้ดูหน้านี้)
  • โทเค็นไม่รู้จักหรือหมดอายุ → รูปภาพข้อผิดพลาดที่แสดงข้อความ Document session not found. Please re-open document. (ไม่พบเซสชันเอกสาร โปรดเปิดเอกสารใหม่)

รูปภาพข้อผิดพลาดเป็นไฟล์ PNG ธรรมดาที่ส่งพร้อมสถานะ 200 เพื่อให้วิดเจ็ตสามารถแสดงแทนหน้าที่ควรจะเป็นได้ การตรวจสอบโดยดูเฉพาะรหัสสถานะจะถือว่ารูปภาพเหล่านี้เป็นหน้าที่ให้บริการ; หน้าปกติมีขนาดหลายสิบถึงหลายร้อยกิโลไบต์, ส่วนรูปภาพข้อผิดพลาดประมาณ 2 KB.

การปิดเอกสาร

csharp
viewer.CloseDocument(token);

CloseDocument จะลบเซสชัน (ปล่อยเอนจินเอกสารและหน่วยความจำทันที), ลบรายการ secure-{token} จากเซสชัน ASP.NET ของคำขอปัจจุบัน, และเพิกถอนโทเค็น. การทำเช่นนี้เป็นทางเลือก — การหมดอายุแบบเลื่อนทำความสะอาดเดียวกัน — แต่สำหรับเอกสารขนาดใหญ่จะช่วยคืนหน่วยความจำทันทีเมื่อผู้ใช้เสร็จสิ้น. จากเบราว์เซอร์, objViewer.Close(true) จะส่งคำขอปิดที่เทียบเท่าไปยัง DocImage.axd.

สิ่งที่ควรจำ

  • เอกสารที่เปิดหนึ่งไฟล์ = เซสชันหนึ่ง = โทเค็นหนึ่ง, มอบให้กับเซสชัน ASP.NET ที่เปิดมัน.
  • โทเค็นหมดอายุตามหน้าต่างเลื่อน; หาก Viewer อยู่นิ่งเกิน DocOptions.TimeOut จะต้องเปิดใหม่.
  • สามารถดึง Viewer มาใช้และแชร์ได้อย่างอิสระ; เซสชันจะถือสถานะทั้งหมด.

หน้านี้เป็นประโยชน์หรือไม่?