Author: sukgamon.s

  • สร้างเอกสาร PDF ด้วย iTextSharp

    ที่มา

    บ่อยครั้งที่ในชีวิตของโปรแกรมเมอร์จะต้องพบกับความต้องการของลูกค้าที่อยากได้รายงานหรือเอกสารที่สามารถสร้างได้จากระบบ แน่นอนว่าประเภทเอกสารที่ต้องการย่อมมี PDF บรรจุไว้แน่นอนเพราะเป็นเอกสารที่นิยมใช้กันอย่างแพร่หลาย ทั้งนี้ เครื่องมือสำหรับการสร้างเอกสารประเภทดังกล่าวมีอยู่มากมาย แต่จุดสำคัญนั้นอยู่ที่การเลือกใช้งานซึ่งย่อมแตกต่างกันไปตามปัจจัยต่าง ๆ เช่นในบทความนี้ ลูกค้าต้องการเอกสารเพื่อพิมพ์เป็น hard copy ไว้ที่หน่วยงาน ยังไม่ถึงขั้นรายงานนะครับ แค่เอกสารบันทึกข้อความ ดังนั้นผู้เขียนจึงไม่เลือกใช้เครื่องมือที่เก่งกาจเช่น Crystal report หรือ Reporting service และทำการค้นหาเครื่องมือที่มีน้ำหนักเบา (เวลาใช้ไม่กินทรัพยากรเยอะ) แต่ตอบสนองความต้องการได้ในขณะนั้น รวมไปถึงการมี documentation ที่ดี เข้าใจง่าย ปฏิบัติตามได้ไม่ยาก ซึ่งสุดท้ายก็มาเจอกับเครื่องมือที่ชื่อ iTextSharp

    คุณสมบัติของเครื่องมือ

    iTextSharp เป็นผลงานของ iText ซึ่งทำมาเพื่อการสร้างเอกสาร PDF บน C# platform โดยเฉพาะ ในขณะเดียวกันก็มีเครื่องมือแบบเดียวกันสำหรับ platform อื่น ๆ ด้วย เราจึงจะได้เห็นตัวอย่างใน documentation ของเขาเป็นภาษา Java

    ความสามารถของ iTextSharp คือการสร้างเอกสาร PDF รวมไปถึงการตัดต่อเอกสารด้วย เช่น การนำเอกสารมารวมกัน การแยกเอกสารออกจากกัน นับว่าเป็นเครื่องมือที่ครบเครื่องทีเดียวสำหรับจุดประสงค์หลัก ๆ ของเรา แต่ความสามารถอื่น ๆ ก็ยังมีครับแต่ผู้เขียนยังไม่ได้ต้องการใช้งานความสามารถนั้นจริง ๆ จึงยังไม่ได้นำเสนอในบทความนี้

    การใช้งานเครื่องมือนี้จะต้องใช้การเขียนโปรแกรม 100% ครับหรือเรียกเป็นภาษาอังกฤษว่า Programmatically ซึ่งยุ่งยากพอควรทีเดียวโดยเฉพาะการจัดวางตำแหน่งของแต่ละส่วน พูดได้ว่าจะต้องจินตนาการหรือร่างแบบลงบนกระดาษเลยทีเดียว ทั้งนี้ทั้งนั้นนี่ก็เป็นข้อดีข้อหนึ่งสำหรับคนชอบเขียนโปรแกรมเพราะเห็นกระบวนการชัดเจน (ไม่ค่อยสะดวกแต่สนุกดี)

    ทดลองใช้

    ติดตั้งโปรแกรม

    ก่อนอื่นเลยก็ต้อง download library มาก่อนครับ (Link)การติดตั้งนั้นไม่ยาก แค่ reference ไปหา dll ที่เค้าให้มาก็พอ ไฟล์ที่ได้มามีทั้งหมด 3 ชุดครับคือ

    1. itextsharp-dll-core
    2. itextsharp-dll-pdfa
    3. itextsharp-dll-xtra

    ทั้งหมดนี้ทำหน้าที่แตกต่างกันครับ สำหรับการสร้าง PDF เราใช้แค่ตัว itextsharp-dll-core ก็พอ

    องค์ประกอบพื้นฐาน

    องค์ประกอบพื้นฐานของ itextsharp นั้นมีหลายตัว แต่ตัวที่เพียงพอสำหรับความต้องการของผู้เขียนในตอนนี้มีดังนี้ครับ

    1. Chunk เป็นองค์ประกอบสำหรับ “คำ”
    2. Phrase คือ “ประโยค” ซึ่งประกอบด้วยหลาย Chunk
    3. Paragraph คือ “ย่อหน้า” ซึ่งประกอบด้วยหลาย phrase และ chunk
    4. PdfPCell คือ cell ในตาราง
    5. PdfPTable คือ ตารางประกอบด้วยหลาย PdfPCell
    6. iTextSharp.text.Image คือรูปภาพ

    การกำหนด font และการเพิ่ม font

    iTextSharp นั้นสามารถเพิ่ม font ได้ครับ โดยการทำตามขั้นตอนดังนี้:

    BaseFont bf_bold = BaseFont.CreateFont(HttpContext.Current.Server.MapPath(“~/Regist/Theme/fonts/THSarabunNewBold.ttf”), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    เท่านี้เราก็จะมี font แบบที่เราอยากได้ไว้ใช้งานในเอกสารครับ หลังจากนั้น เราก็สร้างตัวอักษรเพื่อใช้งาน ตามตัวอย่างนี้ครับ

    // Bold
    BaseFont bf_bold = BaseFont.CreateFont(HttpContext.Current.Server.MapPath(“~/Regist/Theme/fonts/THSarabunNewBold.ttf”), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    h1 = new Font(bf_bold, 18);
    bold = new Font(bf_bold, 16);
    smallBold = new Font(bf_bold, 14);

    // Normal
    BaseFont bf_normal = BaseFont.CreateFont(HttpContext.Current.Server.MapPath(“~/Regist/Theme/fonts/THSarabunNew.ttf”), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    normal = new Font(bf_normal, 16);
    smallNormal = new Font(bf_normal, 14);

    เริ่มต้นสร้างเอกสาร PDF

    เมื่อเรากำหนด font ไว้ใช้งานเสร็จแล้ว เราก็สามารถสร้างเอกสารได้เลยครับ  การสร้างเอกสารจะเริ่มจากการกำหนดขนาดเอกสารครับ ตามตัวอย่างนี้

     

    Document pdfDoc = new Document(PageSize.A4, 30, 30, 20, 20);
    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();

     

    โดยตัวเลข 4 ตัวหลัง layout ของกระดาษคือระยะขอบครับ

    เริ่มใส่องค์ประกอบลงในเอกสาร

    ตัวเอกสารของผมจะเริ่มจากส่วนที่เป็นหัวของเอกสารก่อน ซึ่งจะประกอบด้วยรูปภาพ 1 รูปและข้อความ 1 ข้อความ แต่ปัญหาอยู่การจัดการ layout เพราะเมื่อเราใส่องค์ประกอบเข้าไปมันจะชิดทางด้านซ้ายให้โดยอัตโนมัติ ในกรณีนี้จึงต้องใช้ PdfPTable มาช่วยจัดส่วนหัวเอกสารก่อนครับ ทำการกำหนดขนาดของตารางแล้วจัดองค์ประกอบเข้าไปอยู่ในแต่ละ cell

    private PdfPTable GetHeader() {

    PdfPTable headerTable = new PdfPTable(2);
    headerTable.TotalWidth = 530f;
    headerTable.HorizontalAlignment = 0;
    headerTable.SpacingAfter = 20;
    //headerTable.DefaultCell.Border = Rectangle.NO_BORDER;

    float[] headerTableColWidth = new float[2];
    headerTableColWidth[0] = 220f;
    headerTableColWidth[1] = 310f;

    headerTable.SetWidths(headerTableColWidth);
    headerTable.LockedWidth = true;

    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagePath + “/thai_gov.png”);
    png.ScaleAbsolute(40, 40);

    PdfPCell headerTableCell_0 = new PdfPCell(png);
    headerTableCell_0.HorizontalAlignment = Element.ALIGN_LEFT;
    headerTableCell_0.Border = Rectangle.NO_BORDER;
    headerTable.AddCell(headerTableCell_0);

    PdfPCell headerTableCell_1 = new PdfPCell(new Phrase(“บันทึกข้อความ”, h1));
    headerTableCell_1.HorizontalAlignment = Element.ALIGN_LEFT;
    headerTableCell_1.VerticalAlignment = Element.ALIGN_BOTTOM;
    headerTableCell_1.Border = Rectangle.NO_BORDER;
    headerTable.AddCell(headerTableCell_1);

    return headerTable;
    }

    เมื่อทำการเพิ่มเข้าไปเอกสารแล้วจะได้ลักษณะตามภาพ

    pdf_header

    หลังจากนั้นผมก็เริ่มใส่องค์ประกอบที่เป็นรายละเอียดของส่วนหัวของเอกสารดังนี้

    private PdfPTable GetHeaderDetail() {
    PdfPTable table = new PdfPTable(2);
    table.TotalWidth = 530f;
    table.HorizontalAlignment = 0;
    table.SpacingAfter = 10;

    float[] tableWidths = new float[2];
    tableWidths[0] = 400f;
    tableWidths[1] = 130f;

    table.SetWidths(tableWidths);
    table.LockedWidth = true;

    Chunk blank = new Chunk(” “, normal);

    Phrase p = new Phrase();

    p.Add(new Chunk(“ส่วนราชการ”, bold));
    p.Add(new Chunk(blank));
    p.Add(new Chunk(“วิเทศสัมพันธ์”, normal));

    PdfPCell cell0 = new PdfPCell(p);
    cell0.Border = Rectangle.NO_BORDER;

    table.AddCell(cell0);

    p = new Phrase();
    p.Add(new Chunk(“โทร”, bold));
    p.Add(new Chunk(blank));
    p.Add(new Chunk(“2012”, normal));

    PdfPCell cell1 = new PdfPCell(p);
    cell1.Border = Rectangle.NO_BORDER;

    table.AddCell(cell1);

    p = new Phrase();
    p.Add(new Chunk(“ที่ มอ”, bold));
    p.Add(new Chunk(blank));
    p.Add(new Chunk(master.ApplicationNo, normal));

    cell0 = new PdfPCell(p);
    cell0.Border = Rectangle.NO_BORDER;

    table.AddCell(cell0);

    p = new Phrase();
    p.Add(new Chunk(“วันที่”, bold));
    p.Add(new Chunk(blank));
    p.Add(new Chunk(master.CreatedDate, normal));

    cell1 = new PdfPCell(p);
    cell1.Border = Rectangle.NO_BORDER;

    table.AddCell(cell1);

    p = new Phrase();
    p.Add(new Chunk(“เรื่อง”, bold));
    p.Add(new Chunk(blank));
    p.Add(new Chunk(“ขออนุมัติเดินทางไปต่างประเทศ”, normal));

    cell0 = new PdfPCell(p);
    cell0.Border = Rectangle.NO_BORDER;
    cell0.Colspan = 2;

    table.AddCell(cell0);

    return table;
    }

    เช่นเคยครับ เราจะต้องใช้ Table มากำหนด layout ของเอกสาร เมื่อทำการเพิ่มแล้วก็จะได้เอกสารหน้าตาดังนี้

    pdf_header_detail

    หลังจากนั้นก็ใส่องค์ประกอบต่าง ๆ เพิ่มเข้าไปทีละชิ้น ๆ ตาม source code ด้านล่าง

     

    private Paragraph GetBodyHeader()
    {
    Phrase p = new Phrase();

    p.Add(new Chunk(“เรียน “, normal));
    p.Add(new Chunk(“รองอธิการบดีฝ่ายองค์กรสัมพันธ์และสารสนเทศ”, normal));

    Paragraph para = new Paragraph(p);
    para.SpacingBefore = 20;
    para.SpacingAfter = 20;

    return para;
    }

    private Paragraph GetBody() {
    Paragraph para = new Paragraph();

    para.FirstLineIndent = 38.1f;

    para.Add(new Phrase(“ด้วย”, normal));
    para.Add(new Phrase(“งานวิเทศสัมพันธ์”, normal));
    para.Add(new Phrase(“ขออนุมัติให้นักศึกษาจำนวน ” + master.StudentCount + ” คน เดินทางไปราชการต่างประเทศระหว่างวันที่ ” + master.StartDate + ” ถึงวันที่ ” + master.EndDate + ” รวม ” + master.PeriodDay + ” วัน เพื่อดำเนินกิจกรรมดังต่อไปนี้”, normal));

    return para;
    }

    private PdfPTable GetActivities()
    {
    PdfPTable table = new PdfPTable(3);

    table.TotalWidth = 530f;
    table.HorizontalAlignment = 0;
    table.SpacingBefore = 20;
    table.SpacingAfter = 20;

    // ชื่อกิจกรรม
    // สถาบัน
    // ประเทศ

    float[] columnWidths = new float[3];
    columnWidths[0] = 200f;
    columnWidths[1] = 200f;
    columnWidths[2] = 130f;

    table.SetWidths(columnWidths);
    table.LockedWidth = true;

    PdfPCell cell0 = new PdfPCell(new Phrase(“กิจกรรม”, bold));
    cell0.HorizontalAlignment = Element.ALIGN_LEFT;
    cell0.Border = Rectangle.NO_BORDER;
    table.AddCell(cell0);

    PdfPCell cell1 = new PdfPCell(new Phrase(“สภานที่”, bold));
    cell1.HorizontalAlignment = Element.ALIGN_LEFT;
    cell1.Border = Rectangle.NO_BORDER;
    table.AddCell(cell1);

    PdfPCell cell2 = new PdfPCell(new Phrase(“ประเทศ”, bold));
    cell2.HorizontalAlignment = Element.ALIGN_LEFT;
    cell2.Border = Rectangle.NO_BORDER;
    table.AddCell(cell2);

    List<MasterActivity> activity = master.GetActivities();

    foreach (MasterActivity a in activity)
    {
    cell0 = new PdfPCell(new Phrase(a.ActivityNameThai, normal));
    cell0.HorizontalAlignment = Element.ALIGN_LEFT;
    cell0.Border = Rectangle.NO_BORDER;
    table.AddCell(cell0);

    cell1 = new PdfPCell(new Phrase(a.HostName, normal));
    cell1.HorizontalAlignment = Element.ALIGN_LEFT;
    cell1.Border = Rectangle.NO_BORDER;
    table.AddCell(cell1);

    Institution host = Institution.GetById(a.HostId);

    cell2 = new PdfPCell(new Phrase(host.CountryName, normal));
    cell2.HorizontalAlignment = Element.ALIGN_LEFT;
    cell2.Border = Rectangle.NO_BORDER;
    table.AddCell(cell2);
    }

    return table;
    }

    private Paragraph GetBodyFooter()
    {
    Paragraph para = new Paragraph(new Phrase(“จึงเรียนมาเพื่อโปรดพิจารณาอนุมัติด้วย จักเป็นพระคุณยิ่ง”, normal));
    para.FirstLineIndent = 38.1f;
    para.SpacingAfter = 25;
    return para;
    }

    private void GetSignature(Document pdfDoc) {

    Paragraph para;
    Phrase p;
    Chunk dotLine = new Chunk(“……………………………………………”, normal);

    //if (master.LevelId.Equals(“D”))
    //{
    // p = new Phrase(dotLine);
    // p.Add(new Chunk(“หัวหน้าภาควิชา”, normal));
    // para = new Paragraph(p);
    // pdfDoc.Add(para);
    //}

    p = new Phrase(dotLine);
    p.Add(new Chunk(“หัวหน้าภาควิชา”, normal));
    para = new Paragraph(p);
    para.SpacingAfter = 15;
    pdfDoc.Add(para);

    p = new Phrase(dotLine);
    p.Add(new Chunk(“คณบดี”, normal));
    para = new Paragraph(p);
    para.SpacingAfter = 15;
    pdfDoc.Add(para);
    }

     

    private PdfPTable GetStudentList() {

    Phrase p;

    PdfPTable table = new PdfPTable(8);
    table.TotalWidth = 530f;
    table.HorizontalAlignment = 0;
    //table.SpacingAfter = 20;
    //headerTable.DefaultCell.Border = Rectangle.NO_BORDER;

    float[] colWidths = new float[8];
    colWidths[0] = 30f;
    colWidths[1] = 70f;
    colWidths[2] = 70f;
    colWidths[3] = 70f;
    colWidths[4] = 70f;
    colWidths[5] = 70f;
    colWidths[6] = 70f;
    colWidths[7] = 70f;

    table.SetWidths(colWidths);
    table.LockedWidth = true;

    PdfPCell cell;

    p = new Phrase(“รายชื่อผู้เดินทางจาก ” + master.StartDate + ” ถึง ” + master.EndDate, normal);

    cell = new PdfPCell(p);
    cell.Colspan = 8;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Border = Rectangle.NO_BORDER;
    cell.PaddingBottom = 10;
    table.AddCell(cell);

    #region Header

    cell = new PdfPCell(new Phrase(“ที่”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“รหัสนักศึกษา”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“คำนำหน้า”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“ชื่อ”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“สกุล”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“คณะ”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“วันที่เริ่ม”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(“วันที่สิ้นสุด”, smallBold));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    #endregion

    #region Data

    List<OutboundApplication> apps = master.GetApplications();

    int i = 0;

    foreach (OutboundApplication app in apps)
    {
    cell = new PdfPCell(new Phrase((i + 1).ToString(), smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.StudentId, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.TitleName, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.Firstname, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.Lastname, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.FacultyNameThai, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.StartDate, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase(app.EndDate, smallNormal));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    table.AddCell(cell);

    i += 1;
    }

    #endregion

    return table;
    }

    และส่วนของ Main program ก็จะเป็นแบบนี้ครับ

    protected void Page_Load(object sender, EventArgs e)
    {
    InitElements();

    try
    {
    // Create PDF document
    Document pdfDoc = new Document(PageSize.A4, 30, 30, 20, 20);
    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();

    pdfDoc.Add(GetHeader());
    pdfDoc.Add(GetHeaderDetail());

    LineSeparator line = new LineSeparator();

    pdfDoc.Add(line);

    pdfDoc.Add(GetBodyHeader());
    pdfDoc.Add(GetBody());
    pdfDoc.Add(GetActivities());
    pdfDoc.Add(GetBodyFooter());
    GetSignature(pdfDoc);
    pdfDoc.NewPage();
    pdfDoc.Add(GetStudentList());

    pdfWriter.CloseStream = false;
    pdfDoc.Close();
    Response.Buffer = true;
    Response.ContentType = “application/pdf”;
    Response.AddHeader(“content-disposition”, “attachment;filename=Example.pdf”);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    }

    สุดท้ายแล้วเราก็จะได้เอกสาร PDF หน้าตาแบบนี้ครับ Example

    บทสรุป

    จะเห็นได้ว่าการสร้างเอกสาร PDF ด้วยเครื่องมือนี้จะกินแรงพอควรนะครับ แต่ข้อดีของ iTextSharp คือน้ำหนักเบาและด้วยการเขียนโค้ดของเรา เราสามารถจะนำอะไรก็ได้ (ตามที่มันมีให้) ไปใส่ในเอกสารได้อย่างอิสระ สำหรับตอนนี้ iTextSharp ยังคงตอบสนองได้ตามความต้องการ แต่เมื่อความต้องการของลูกค้าเปลี่ยนแปลง เราจะได้เห็นกันว่าเครื่องมือตัวนี้จะทรงพลังพอหรือไม่

  • การอัปโหลดไฟล์หลาย ๆ ไฟล์พร้อมกันด้วย Dojo

    ที่มา

    การ upload แฟ้มข้อมูล (File) เป็นกิจกรรมที่อยู่คู่กับ Web application มาเนิ่นนาน ยิ่งเมื่อการพัฒนา Web application เข้าสู่ยุคสมัยของ Web 2.0 และเป็น Web 3.0 ในยุคปัจจุบันก็ยิ่งเกิดเครื่องมือต่าง ๆ เพื่ออำนวยความสะดวกในการ upload ไฟล์มากขึ้นรวมไปถึงความสามารถในการอัปโหลดหลาย ๆ ไฟล์พร้อมกัน (Multi-File Uploading)

    ณ เวลานี้มีเครื่องมือมากมายสำหรับนักพัฒนา Web application  ผู้อ่านอาจมีโอกาสได้ใช้งานเครื่องมือจากบางค่ายบ้างแล้ว แต่ในบทความนี้ผู้เขียนจะแนะนำวิธีการใช้งานเครื่องมือในการทำให้ Web application ของเราสามารถรองรับความต้องการในการอัปโหลดไฟล์หลาย ๆ ไฟล์ได้พร้อมกันตัวหนึ่งชื่อ HTML5 Multi-File Uploader ของ Dojo Toolkit ซึ่งทำงานได้ตามความต้องการของผู้เขียนดังนี้

    ความต้องการ

    1. สามารถ upload file ได้หลาย ๆ ไฟล์พร้อมกัน
    2. upload file ด้วย AJAX
    3. ต้องไม่มีการ Post Back หรือทำให้เกิดการ Load หน้า Web ใหม่
    4. สามารถปรับแต่งได้ตามความต้องการ

    ที่จริงแล้วเครื่องมือของ Dojo Toolkit เป็นเครื่องมือที่ใช้ยากพอสมควรสำหรับคนที่ไม่ค่อยถนัดในการใช้งาน JavaScript แต่เนื่องจาก API ที่เปิดโอกาสให้ผู้ใช้ได้เข้าถึงทุกส่วนของ Widget ทำให้เราสามารถปรับแต่งการใช้งานเพื่อให้เหมาะกับความต้องการได้โดยอิสระ

    ศึกษาคุณสมบัติของเครื่องมือ

    HTML5 Multi-File Uploader เป็นส่วนหนึ่งใน package ชื่อ dojox (dojox/form/Uploader) ซึ่งในความเป็นจริงแล้ว เครื่องมือตัวนี้ไม่ได้เจาะจงเฉพาะการใช้งาน HTML5 เท่านั้นแต่ยังคงใช้งานกับ Flash หรือ iframe ได้ด้วยตามแต่ผู้ใช้จะปรับแต่งเนื่องจาก Web browser แต่ละยี่ห้อก็มีข้อจำกัดแตกต่างกันไปตาม Browser engine และ Rendering engine

    ตามชื่อของเครื่องมือ Uploader ใช้ input ของ HTML5 เป็นองค์ประกอบหลัก จึงต้องกำหนดใน Form ให้รองรับการอัปโหลดไฟล์ด้วยการกำหนด attribute ชื่อ method และ enctype ดังนี้

     

    <form method=”post” enctype=”multipart/form-data”> 

     

    การสร้าง Widget ของ Uploader ทำได้ 2 รูปแบบดังนี้

    Programmatically

    require([

    “dojox/form/Uploader”

    ], function (Uploader) {

    myUploader = new dojox.form.Uploader();

    }

     

    Markup

    <input name=”uploadedFile” multiple=”true” type=”file” data-dojo-type=”dojox/form/Uploader” data-dojo-props=”label: ‘Select Some Files’,……..” />

    <div id=”files” data-dojo-type=”dojox/form/uploader/FileList” data-dojo-props=”uploaderId: ‘uploader’”></div>

    ลองใช้งาน

    เนื่องจากผู้เขียนพัฒนา Web application ด้วย Visual C# จึงจะยกตัวอย่างการประยุกต์การใช้งาน Uploader ร่วมกับ ASP.NET ซึ่งมีขั้นตอนดังนี้

    1. นำ Widget มาวางในส่วนที่เราต้องการ

    <input name=”uploadedfile” type=”file” id=”uploader” data-dojo-id=”fileUploader” data-dojo-type=”dojox/form/Uploader” data-dojo-props=”label: ‘Select files’, url: ‘../Test/TestUpload.ashx’, multiple: true” />
    <input type=”hidden” name=”hdnMasterId” id=”hdnMasterId” value=”test” />
    <input type=”hidden” name=”hdnFileDesc” id=”hdnFileDesc” value=”test” />
    <div id=”files” data-dojo-type=”dojox/form/uploader/FileList” data-dojo-props=”uploaderId: ‘uploader’”></div>

     

    เมื่อสังเกตุแล้วจะเห็นได้ว่าจะไม่มีปุ่ม Submit เนื่องจากผู้เขียนต้องการ upload file แบบ AJAX จึงไม่ต้องการให้ผู้ใช้กด ENTER แล้วส่งฟอร์มทั้งหมดไปทันที อีกจุดหนึ่งคือผู้เขียนต้องการส่งข้อมูลบางอย่างไปในคราวเดียวกันด้วยคือ  hdnMasterId และ hdnFileDesc

     

    ลอง Run ดูน่าจะได้น่าตาประมาณนี้

    ex1

    เมื่อเราลองเลือก file แล้ว ชื่อ file ที่เราเลือกจะแสดงอยู่ตำแหน่งด้านล่าง

    ex2

    2. กำหนดการทำงานในฝั่ง client

    ในขั้นตอนนี้เราจะต้องเขียน JavaScript เพื่อควบคุมการทำงานของ widget โดยผู้เขียนจะกำหนดให้ทำการ upload file แบบ AJAX เมื่อกดปุ่มใดปุ่มหนึ่งดังนี้

    fileUploader..set(“onComplete”, function(){

    //อะไรก็ตามที่ต้องให้ทำเมื่อทำการ upload เสร็จแล้ว

    });

    fileUploader.submit();

     

    3. กำหนดการทำงานในฝั่ง server

    ในส่วนของฝั่ง server เราก็ต้องมีการกำหนดการทำงานเช่นกัน เช่น การจัดเก็บไฟล์ การดึงข้อมูลจากการส่งข้อมูลแบบ AJAX เนื่องจากเรามีการส่งข้อมูลอื่น ๆ ไปด้วย ทั้งนี้เราไม่จะเป็นจะต้องใช้ web form มารองรับในการ upload เราสามารถใช้ ashx มาทำงานแทนที่ได้เลย การเขียนโปรแกรมฝั่ง server เป็นดังตัวอย่างด้านล่าง

    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = “text/plain”;
    JavaScriptSerializer js = new JavaScriptSerializer();
    string result = String.Empty;

    IDictionary<string, string> postData = new Dictionary<string, string>();

    foreach (string name in context.Request.Form.Keys)
    {
    //context.Response.Write(“<br>KEYS: ” + name);
    postData[name] = context.Request.Form[name];
    }

    //result = js.Serialize(new Response(false, postData[“hdnTest”], String.Empty));
    //context.Response.Write(result);

    string masterId = postData[“hdnMasterId”];
    string description = postData[“hdnFileDesc”];

    try
    {
    int len = context.Request.Files.Count;

    for (int i = 0; i < len; i++)
    {
    HttpPostedFile postedFile = context.Request.Files.Get(i) as HttpPostedFile;

    string[] fileNames = postedFile.FileName.Split(‘\\’);
    string fileName = String.Empty;

    if (fileNames.Length > 1)
    {
    fileName = fileNames[fileNames.Length – 1];
    }
    else
    {
    fileName = fileNames[0];
    }

    string[] fileType = postedFile.FileName.Split(‘.’);

    string uploadPath = context.Server.MapPath(Settings.Default.UploadFilePath);
    string newFileName = “[” + masterId + “]_” + fileName;
    string saveFileName = uploadPath + @”\” + newFileName;
    postedFile.SaveAs(saveFileName);

    OutboundMaster app = OutboundMaster.GetById(masterId);
    app.AddAttachedFile(newFileName, description);

    result = js.Serialize(new BizResponse(true));
    }

    context.Response.Write(result);
    }
    catch (Exception ex)
    {
    result = js.Serialize(new BizResponse(false, ex.Message, ex.StackTrace));
    context.Response.Write(result);
    }
    }

    จะเห็นได้ว่าตัวแปรที่มาจากการ post จะอยู่ในรูปแบบของ array ดังนั้นเราจะต้องทำการหาตัวแปรด้วย key ก่อน จากนั้นก็จะเข้าสู่ขั้นตอนการบันทึกไฟล์ตามที่เราออกแบบ

    ทั้งนี้ตัว Uploader มีข้อกำหนดข้อหนึ่งคือจะต้องมีการตอบสนองจากฝั่ง server หากไม่มีจะทำให้ Uploader แสดง error ถึงแม้ว่าการ upload file นั้นเสร็จสิ้นไปแล้วก็ตาม ดังนั้นที่ไฟล์ ashx จึงต้องมีการส่งข้อมูลกลับมาดังตัวอย่างด้านล่างนี้

    context.Response.Write(……);

     

    บทสรุป

    การใช้งาน Uploader อาจจะซับซ้อนมากน้อยขึ้นอยู่กับคนที่นำไปใช้ และถึงแม้ว่าเครื่องมือตัวนี้จะไม่ค่อย compatibility กับ Visual Studio เนื่องจากแนวคิดในการทำงานนั้นแตกต่างกัน แต่ยังคงสามารถนำมาประยุกต์ใช้งานเพื่อให้ได้ผลลัพธ์ตามที่ต้องการได้ และเนื่องจาก Dojo Toolkit นั้นเปิดเผย source code ทำให้เราสามารถทำความเข้าใจและจุดประกายแนวคิดใหม่ ๆ ได้อีกด้วย

  • รู้จักกับ Algebraic Specification

    สวัสดีครับ

    หลังจากใช้เวลานานในการคิดว่าจะเขียนอะไรดี ก็คิดได้ว่านำเอาความรู้ที่ได้จากการไปเรียนทบทวนเรื่องวิศวกรรมซอฟต์แวร์มาเล่าสู่กันฟังดีกว่า เรื่องที่จะเล่านี้ก็คือ การเขียน Specification ในรูปแบบหนึ่งซึ่งอาจจะใหม่สำหรับบางคน เรียกว่า Algebraic Specification (ชื่อภาษาไทยว่าอะไร ไปหาเอาเองนะครับ)

    Algebraic Specification เป็นรูปแบบหนึ่งของ Formal Specification (อย่าเพิ่งงงนะครับ) ในการเขียน specification รูปแบบนี้จะใช้คณิตศาสตร์เข้ามาช่วยเพื่อให้เห็นการติดต่อระหว่าง module หรือ component ที่ชัดเจน

    แล้วมันต่างจากที่เขียนอยู่ยังไง?

    ถ้าคุณเขียน specification พรรณนาด้วยภาษามนุษย์ยาวเป็นหน้า ๆ อันนั้นเรียกว่า form-based specification ครับ เป็นวิธีการเขียนที่มนุษย์อ่านแล้วเข้าใจมากกว่า แต่ในการใช้ภาษาเขียนนั้น อาจจะเกิดความกำกวมได้ครับ ประเภทที่คนอ่าน อ่านแล้วงง อ่านแล้วรู้สึกว่ามีหลายความหมายหรือเข้าใจไม่ชัดเจนซึ่งอาจเป็นผลมาจาก Requirement definition และ Requirement specification ที่กำกวมเช่นเดียวกัน

    ดังนั้นจึงมีผู้ปราดเปรื่องบางท่านคิด Algebraic specification ขึ้นมาซึ่งหน้าตาเป็นดังนี้

    ARRAY (Elem: [Undefined -> Elem])
    sort ArrayImports INTEGER
    DescriptionArrays are collections of elements of generic type Elem. They have a lower and upper bound (discovered by the operations First and Last). Individual elements are accessed via their numeric index.Create takes the array bounds as parameters and creates the array, initializing its values to Undefined. Assign creates a new array which is the same as its input with the specified element assigned the given value. Eval reveals the value of a specified element. If an attempt is made to access a value outside the bounds of the array, the value is undefined.
    OperationsCreate (Integer, Integer) -> ArrayAssign (Array, Integer, Elem) -> Array

    First (Array) -> Integer

    Last (Array) -> Integer

    Eval (Array, Integer) -> Elem

    AxiomsFirst (Create (x, y)) = xFirst (Assign (a, n, v)) = First (a)

    Last (Create (x, y)) = y

    Last (Assign (a, n, v)) = Last (a)

    Eval (Create (x, y), n) = Undefined

    Eval (Assign (a, n, v), m) =

    if m < First (a) or m > Last (a) then Undefined

    else

    if m = n then v

    else Eval (a, m)

    จะสังเกตได้ว่าอย่างไรเสียก็ต้องมีคำอธิบายที่มนุษย์เข้าใจอยู่บ้างใช่ไหมครับ แต่ในส่วนที่เพิ่มเข้ามาคือ operations และ axioms  การใช้คณิตศาสตร์ทำให้เราสามารถเห็น input output ของแต่ละการทำงานชัดเจน ลองเปรียบเทียบสองตัวอย่างนี้ดูนะครับ

    “สิ่งมีชีวิตสามารถกินอะไรก็ได้ที่กินได้”

    Eat (Life, Eatable)

    ทั้งนี้ที่ Algebraic specification ยังไม่เป็นที่นิยมก็เพราะว่าผู้ใช้เข้าใจยากครับ ตอนทวนสอบความต้องการอาจจะต้องอธิบายกันหัวหมุน แต่ข้อดีของมันก็น่าทึ่งเพราะคนที่อ่านเข้าใจก็จะเห็นส่วนการติดต่อเชื่อมโยงกันชัดเจนระหว่าง operation  module  หรือ  component  ทั้งนี้ผู้เขียนกำลังคิดอยู่ว่าจะเอาเจ้าสิ่งนี้มาทำประโยชน์ได้อย่างไร ถ้าคิดออกแล้วจะเอามาเล่าให้ฟังนะครับ