Created
February 22, 2017 11:47
-
-
Save witoong623/bcb6ec5666f84fdef4e2191672689c81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Windows.Forms; | |
using testClientBinaryWebservice01.service1; | |
namespace testClientBinaryWebservice01 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void btnOpenFile_Click(object sender, EventArgs e) | |
{ | |
var fileDialog = new OpenFileDialog(); | |
string filePath = null; | |
if (fileDialog.ShowDialog() == DialogResult.OK) | |
{ | |
filePath = fileDialog.FileName; | |
} | |
else | |
{ | |
// เปิดแล้วไม่กด OK ก็ให้ไปเปิดใหม่ | |
return; | |
} | |
// ต้องใช้อะไรก็ได้ ที่อ่านไฟล์แล้ว return byte[] ออกมา ถ้าอ่านเป็น string ก็ทำได้ แต่ตามหลักจะไม่ได้ผลกับไฟล์ที่เป็น binary ไฟล์(ไม่เคยลองว่าจริงๆ ทำได้ไหม) | |
FileStream objfilestream = new FileStream(filePath, FileMode.Open, FileAccess.Read); | |
// ต้องรู้ขนาดไฟล์ก่อน เพื่อจะเตรียม byte[] ที่ขนาดพอดี | |
int len = (int)objfilestream.Length; | |
byte[] fileBinaries = new byte[len]; | |
// สำคัญที่สุด ต้องอ่านไฟล์เป็น binary ใน C# คือ byte | |
objfilestream.Read(fileBinaries, 0, len); | |
Service1 myservice = new Service1(); | |
// โยน binary ทั้งหมดให้ server | |
myservice.SaveDocument(fileBinaries, filePath.Remove(0, filePath.LastIndexOf("\\") + 1)); | |
objfilestream.Close(); | |
lblResult.Text = "อัพแล้ว"; | |
lblFilename.Text = filePath; | |
} | |
private void btnLoad_Click(object sender, EventArgs e) | |
{ | |
// เอาชื่อไฟล์ที่โดนกรอกมา | |
string fineName = txtRequestFile.Text; | |
// สร้าง FileStream เตรียมไว้รอเขียนไฟล์นั้น | |
FileStream objfilestream = new FileStream(fineName.Insert(fineName.LastIndexOf("."), "2"), FileMode.Create, FileAccess.ReadWrite); | |
Service1 myservice = new Service1(); | |
// ต้องเอาขนาดไฟล์มาก่อน เพื่อที่จะได้เตรียมขนาด byte[] ถูกต้องและเขียนไฟล์ด้วยข้อมูลเท่านั้นจริงๆ | |
int len = myservice.GetDocumentLen(fineName.Remove(0, fineName.LastIndexOf("\\") + 1)); | |
byte[] mybytearray = new byte[len]; | |
mybytearray = myservice.GetDocument(fineName.Remove(0, fineName.LastIndexOf("\\") + 1)); | |
// เขียน binary ลงไฟล์ทั้งหมด | |
objfilestream.Write(mybytearray, 0, len); | |
objfilestream.Close(); | |
lblResult.Text = "โหลดแล้ว"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment