剛開始工作...要做一個Web Service和 Windows Service
完全沒碰過的我花了幾天鑽研...順便回憶寫程式
其實就只是一個很簡單的檔案搬移跟上傳...怎麼會開這麼多條件呢...~_~
以下就只有Method的部分, 但應該也夠了
客倌假如真的要注意...就是Web.Config的設定
沒PO在這邊是因為太多自動產生的文字了
所以提示一下, 加入AppSettings就好:)
[WebMethod]
public string HelloWorld() {
return Server.MapPath("~/upload");
}
[WebMethod]
public bool UploadFile(string FileName, string FolderName, byte[] Data, string PassWord)
{
//取得暫定的儲存路徑(暫定是/upload)與通關密碼
string SavePath = WebConfigurationManager.AppSettings["SavePath"];
string ServicePW = WebConfigurationManager.AppSettings["ServicePW"];
if (PassWord != ServicePW)
{
return false;
}
try
{
MemoryStream memoryStream = new MemoryStream(Data);
//依照FolderName決定儲存位置
switch (FolderName)
{
case "A":
SavePath = WebConfigurationManager.AppSettings["A"];
break;
case "B":
SavePath = WebConfigurationManager.AppSettings["B"];
break;
case "C":
SavePath = WebConfigurationManager.AppSettings["C"];
break;
}
//若沒有config檔中的SavePath要另外建立
if (!Directory.Exists(Server.MapPath(SavePath)))
{
Directory.CreateDirectory(Server.MapPath(SavePath));
}
FileStream fileStream = new FileStream(Server.MapPath(SavePath+ "/" + FileName), FileMode.Create);
memoryStream.WriteTo(fileStream);
fileStream.Close();
memoryStream.Close();
fileStream = null;
memoryStream = null;
return true;
}
catch (Exception)
{
return false;
}
}
}
留言列表