ASP.NET通過Remoting service上傳文件
最近在因?yàn)樵趯W(xué)習(xí)Remoting,純粹只是了解一下,發(fā)現(xiàn)Remoting確實(shí)是好東西。
我們通常有三種方式來使用remoting,一種是
第一種:Publishing a public object
公開的對象創(chuàng)建在本地
第二種:Remote creation of a public object (SAO)
對象創(chuàng)建在客戶端請求中
第三種:Remote creation of a private object (CAO)
對象創(chuàng)建在HOST上,客戶端引用服務(wù)器上的對象
目次我也沒有很好理解這三種的本質(zhì)區(qū)別在哪里。而這三種方式的remoting創(chuàng)建方式也不相同。
第一種方式
Host:
ChannelServices.RegisterChannel (new TcpChannel(1500));
cTransfer Trans = new cTransfer();
RemotingServices.Marshal (Trans, "TestService");Client:
cTransfer T = (cTransfer) Activator.GetObject(typeof(cTransfer),
"tcp://host:1500/TestService");
第二種方式
Host:
ChannelServices.RegisterChannel (new TcpChannel(1500));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(cTransfer),
"TestService", WellKnownObjectMode.Singleton);Client:
cTransfer T = (cTransfer) Activator.GetObject(typeof(cTransfer),
"tcp://host:1500/TestService");
第三種方式
Host:
ChannelServices.RegisterChannel (new TcpChannel(1500));
RemotingConfiguration.RegisterActivatedServiceType(typeof(cTransfer));Client:
object[] attr = {new UrlAttribute("tcp://host:1500")};
object[] args = {"Sample constructor argument"};
cTransfer T = (cTransfer) Activator.CreateInstance(typeof(cTransfer), args, attr);
如果我們需要一個對象(object)允許遠(yuǎn)程調(diào)用處理,那么這個對象(object)需要繼承于MarshalByRefObject這個類。
如何在remoting中傳送文件呢?基本思路就是在client打開client的文件,轉(zhuǎn)換在Byte[]類型之后調(diào)用host的對象。
Client與Host之間傳送的對象
[Serializable]
public struct kAction
{
public string filename;
public byte[] context;
};打開文件,將流字節(jié)保存到Context中去
Stream fileStream=File.Open(this.transFileName.Text,FileMode.Open);
fileStream.Position=0;
byte[] Content = new byte[((int) fileStream.Length) + 1];
fileStream.Read(Content,0,Content.Length) ;
在Host在讀取到Kaction之后,把它保存到指定文件夾下面
MemoryStream meoeryStream=new MemoryStream(k_Action.context);
FileStream fileStream=new FileStream(@"d:\"+k_Action.filename,FileMode.Create);
meoeryStream.WriteTo(fileStream);
fileStream.Close();
meoeryStream.Close();
發(fā)現(xiàn)不能在對象中又定義新的對象。在準(zhǔn)備發(fā)送到HOST上會提示“包含潛在危險的類型”。
[Serializable]
public struct kAction
{
public string filename;
public byte[] context;
public FineInfo fileInfo;//這里
};
記錄一下自己的心得。有空我會好好整理下下回做篇完整點(diǎn)的。
cnzc's blogs
相關(guān)文章
基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼
這篇文章主要介紹了基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02ASP.NET中DES加密與解密MD5加密幫助類的實(shí)現(xiàn)代碼
這篇文章主要介紹了ASP.NET中DES加密與解密MD5加密幫助類的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-07-07ASP.NET Core使用JWT自定義角色并實(shí)現(xiàn)策略授權(quán)需要的接口
這篇文章介紹了ASP.NET Core使用JWT自定義角色并實(shí)現(xiàn)策略授權(quán)需要的接口,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01ASP.NET Core中快速構(gòu)建PDF文檔的步驟分享
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中快速構(gòu)建PDF文檔的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12在asp.net中實(shí)現(xiàn)datagrid checkbox 全選的方法
在asp.net中實(shí)現(xiàn)datagrid checkbox 全選的方法...2006-12-12