通过ax2012将xml文件上传到ftp服务器

时间:2022-07-28 03:46:30

i'm looking for to connect to ftp via dynamics ax 2012 and upload file xml on the ftp server, i have do that job for test , but i can 't connect to ftp i get this error Impossible de créer l'objet 'CLRObject'

我正在寻找通过动态ax 2012连接到ftp并在ftp服务器上传文件xml,我已经做了测试工作,但我无法连接到ftp我得到这个错误Impossibledecréerl'objet'CLRObject “

static void uploadTestFile(Args _args) {
   System.Object ftpo;
   System.Object ftpResponse;
   System.Net.FtpWebRequest request;
   System.IO.StreamReader reader;
   System.IO.Stream requestStream;
   System.Byte[] bytes;
   System.Net.NetworkCredential credential;
   System.String xmlContent;
   System.Text.Encoding utf8;
   System.Net.FtpWebResponse response;
   ;
   // Read file
   reader = new System.IO.StreamReader("ftp://net.sites.com/user/test.xml");
   utf8 = System.Text.Encoding::get_UTF8();
   bytes = utf8.GetBytes( reader.ReadToEnd() );
   reader.Close();
   // little workaround to get around the casting in .NET
   ftpo = System.Net.WebRequest::Create("ftp://net.sites.com/user/test.xml");
   request = ftpo;

   credential = new System.Net.NetworkCredential("login","pass");
   request.set_Credentials(credential);
   request.set_ContentLength(bytes.get_Length());
   request.set_Method("STOR");
   // "Bypass" a HTTP Proxy (FTP transfer through a proxy causes an exception)
   // request.set_Proxy( System.Net.GlobalProxySelection::GetEmptyWebProxy() );
   requestStream = request.GetRequestStream();
   requestStream.Write(bytes,0,bytes.get_Length());
   requestStream.Close();

   ftpResponse = request.GetResponse();
   response = ftpResponse;
   info(response.get_StatusDescription());
} 

2 个解决方案

#1


0  

Quote from this blog

从这个博客引用

Once more axaptapedia.com saves the day. This time pointing us to the FtpWebRequest class in the .NET framework inside Dynamics AX. The Axaptapedia page is complete except for one wafer-thin omission... Exception handling.

axaptapedia.com再次拯救了这一天。这一次将我们指向Dynamics AX内部.NET框架中的FtpWebRequest类。 Axaptapedia页面是完整的,除了一个薄薄的遗漏...异常处理。

Comparing your code with that on the blog I'd say that it does not apply for InteropPermission? Also the blog mentiones executing on the server, which may avoid other potential errors such as not having ftp.exe installed on the system... You HAVE checked that an FTP client exists on the machine, right?

将您的代码与博客上的代码进行比较我会说它不适用于InteropPermission?此外,博客还提到在服务器上执行,这可能会避免其他潜在的错误,例如系统上没有安装ftp.exe ......你已经检查过机器上是否存在FTP客户端,对吧?

#2


0  

The problem is with the StreamReader initialisation:

问题在于StreamReader初始化:

reader = new System.IO.StreamReader("ftp://net.sites.com/user/test.xml");

If the source file is coming from FTP, you can rewrite this section as follows:

如果源文件来自FTP,您可以按如下方式重写此部分:

System.IO.Stream responseStream;

ftpo = System.Net.WebRequest::Create("ftp://net.sites.com/user/testFrom.xml");
request = ftpo;

credential = new System.Net.NetworkCredential("login","pass");
request.set_Credentials(credential);
request.set_Method("RETR");

response = request.GetResponse();
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream);

#1


0  

Quote from this blog

从这个博客引用

Once more axaptapedia.com saves the day. This time pointing us to the FtpWebRequest class in the .NET framework inside Dynamics AX. The Axaptapedia page is complete except for one wafer-thin omission... Exception handling.

axaptapedia.com再次拯救了这一天。这一次将我们指向Dynamics AX内部.NET框架中的FtpWebRequest类。 Axaptapedia页面是完整的,除了一个薄薄的遗漏...异常处理。

Comparing your code with that on the blog I'd say that it does not apply for InteropPermission? Also the blog mentiones executing on the server, which may avoid other potential errors such as not having ftp.exe installed on the system... You HAVE checked that an FTP client exists on the machine, right?

将您的代码与博客上的代码进行比较我会说它不适用于InteropPermission?此外,博客还提到在服务器上执行,这可能会避免其他潜在的错误,例如系统上没有安装ftp.exe ......你已经检查过机器上是否存在FTP客户端,对吧?

#2


0  

The problem is with the StreamReader initialisation:

问题在于StreamReader初始化:

reader = new System.IO.StreamReader("ftp://net.sites.com/user/test.xml");

If the source file is coming from FTP, you can rewrite this section as follows:

如果源文件来自FTP,您可以按如下方式重写此部分:

System.IO.Stream responseStream;

ftpo = System.Net.WebRequest::Create("ftp://net.sites.com/user/testFrom.xml");
request = ftpo;

credential = new System.Net.NetworkCredential("login","pass");
request.set_Credentials(credential);
request.set_Method("RETR");

response = request.GetResponse();
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream);