天天看點

winform界面嵌入dwg圖紙_在C# winform程式中怎樣上傳CAD檔案的圖紙,并調用AUTOCAD軟體進行編輯圖紙。....

我可以用以下方法,以下方法是上傳圖檔的,你上傳cad檔案是一樣的。

因為WinForm都是運作在本地的,而我們的網站一般都是布署在伺服器上,運作在伺服器上的,是以在網站上面上傳檔案,就好似于儲存檔案到本地。但在WinForm上就不一樣了,本章我們簡單舉一個在WinForm利用WebService上傳檔案到伺服器的方法:

首先們先建立一個WebService服務,該服務包含一個UpdateFile方法,該方法接收兩個byte[]與string類型參數。該方法非常簡單,就是按照string參數指定的路徑和名稱将byte[]參數值儲存到伺服器,代碼如下:

[WebService(Namespace = " http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

public class WebService : System.Web.Services.WebService

{

[WebMethod]

public bool UpdateFile(byte[] content, string pathandname)

{

File.WriteAllBytes(Server.MapPath(pathandname), content);

}

}

為了安全,我們可以驗證一下pathandname的值,使其隻儲存圖檔格式的檔案。全部代碼如下:

[WebService(Namespace = " http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

public class WebService : System.Web.Services.WebService

{

[WebMethod]

public bool UpdateFile(byte[] content, string pathandname)

{

int index = pathandname.LastIndexOf(".");

if (index == 0)

{

return false;

}

else

{

string extended = string.Empty;

if (index + 1 == pathandname.Length)

{

return false;

}

else

{

extended = pathandname.Substring(index + 1);

if (extended == "jpeg" || extended == "gif" || extended == "jpg")

{

try

{

File.WriteAllBytes(Server.MapPath(pathandname), content);

return true;

}

catch (Exception ex)

{

return false;

}

}

else

{

return false;

}

}

}

}

}

好了,建立完WebService後,将它布署到伺服器上面,然後在Winform中添加對該服務的引用,添加方法如下:

在winform項目的引用-添加服務引用,在打開的對話框的位址欄中添加布署好的WebService位址,點選前往,驗證通過後即添加成功了