[DevExpress] XtraReport – XRPictureBox Relative ImageUrl Path

สำหรับวันนี้มาเจอ Code Report หลังจากเผามา 3-4 เดือนที่ผ่านมา แล้วพบว่าทุก Report รูป Logo Fixed Path หมดเลย ถ้าดูจาก xml ของ report พบว่า

# File Report/CTDInvoiceReceipt.repx

<Item2 Ref="9" ControlType="XRPictureBox" Name="pictureBox1" ImageUrl="C:\ฺNineYCom\CTD\Report\Logo\Logo_Landscape.jpg" Sizing="ZoomImage" SizeF="100,50" LocationFloat="0,10" />

ถ้าดูจาก Code พบว่ามันจะ Lock Path C:\ฺNineYCom\CTD\Report\Logo\Logo_Landscape.jpg ถ้าเกิดต้องไปลองที่อื่น หรือใช้เป็น Container มันจะแบบเราหา Drive C ได้ที่ไหน ? มีแต่ /var /usr เป็นต้น

หลังจากลอง Research พบว่าตัว DevExpress มันบอก Base Path แล้วไป map เป็น แบบ Relative Path ได้ครับ โดยไปเพิ่ม "DXResourceDirectory" แล้ว Load เข้ามา และเขียน Code ที่ main หรือ program.cs เพื่อกำหนด ดังนี้

// Set the resource directory to the MyResources folder in the user profile.
string dataPath = "Path to Your Report Resource"
AppDomain.CurrentDomain.SetData("DXResourceDirectory", dataPath);

หรือ แบบ webapi เขียนเต็มๆ ตามนี้

var app = builder.Build();

// Set the resource directory
string contentPath = env.ContentRootPath;
AppDomain.CurrentDomain.SetData("DXResourceDirectory", contentPath);

app.Run();

จากนั้นที่ Code ของ Report ไปแก้ ImageUrl จาก Full Path มาเป็นแบบ Relative Path ตามรูป

สำหรับวิธีนี้ข้อดี App ของเรา Dynamic มากขึ้น ไม่ต้องมา Fixed Path ลงทุกที่

แต่ข้อเสียมันมีเหมือนกัน ต้องตรวจสอบนิดนึงมัน เปิดช่องให้ดึง Data อาจจะอันตรายมาด้วย แต่มีแนวทางแนะนำตามนี้

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.