Code: C# 透過 Web Services 達到 PDA 上傳、下載檔案的功能

C# 透過 Web Services 達到 PDA 上傳、下載檔案的功能

Web Services

[WebMethod] public string FileUpload(byte[] f,string filename)
{
MemoryStream ms = new MemoryStream(f);
FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(“~/files/”) + filename, FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();

//return “ok”;
return System.Web.Hosting.HostingEnvironment.MapPath(“~/files/”).ToString();
}

[WebMethod] public string FileDownload(string fn)
{
string filename = System.Web.Hosting.HostingEnvironment.MapPath(“~/files/”) + fn;
FileStream fs;
try
{
fs = new FileStream(filename, FileMode.Open);
}
catch(Exception ex)
{
return “”;
}
BinaryReader br = new BinaryReader(fs);
byte[] data = br.ReadBytes((int)fs.Length);
fs.Close();
br.Close();
return ToHexString(data);
}

public static string ToHexString(byte[] bytes) // 0xae00cf => “AE00CF ”
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder str= new StringBuilder();

for (int i = 0; i < bytes.Length; i++) { str.Append(bytes[i].ToString("X2")); } hexString = str.ToString(); } return hexString; }

裝置端

fl_up.file_upload a = new fl_up.file_upload();
a.Timeout = -1;
string str = a.FileDownload(PCDs.Tables[“file”].Rows[i][“file_name”].ToString());
byte[] data = GetBytes(str, 0);

MemoryStream ms = new MemoryStream(data);
FileStream fs = new FileStream(myDocumentsPath + “\\” + PCDs.Tables[“file”].Rows[i][“file_name”].ToString(), FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();

public static byte[] GetBytes(string newString, int discarded)
{
discarded = 0;

int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i = 0; i < bytes.Length; i++) { hex = new String(new Char[] { newString[j], newString[j + 1] }); bytes[i] = HexToByte(hex); j = j + 2; } return bytes; } private static byte HexToByte(string hex) { if (hex.Length > 2 || hex.Length <= 0) throw new ArgumentException("hex must be 1 or 2 characters in length"); byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber); return newByte; }

在〈Code: C# 透過 Web Services 達到 PDA 上傳、下載檔案的功能〉中有 1 則留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *