C#Ftp在Windows 10通用应用中上传?

编程入门 行业动态 更新时间:2024-10-28 21:23:59
本文介绍了C#Ftp在Windows 10通用应用中上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用系统;我想使用ftp上传Windows 10应用程序中的文件,我尝试了下面的代码。 使用System.IO; 使用System.Net; 使用System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main() { //获取用于与服务器通信的对象。 FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftp://www.contoso/test.htm); request.Method = WebRequestMethods.Ftp.UploadFile; //此示例假定FTP站点使用匿名登录。 request.Credentials = new NetworkCredential(anonymous,janeDoe@contoso); //将文件的内容复制到请求流中。 StreamReader sourceStream = new StreamReader(testfile.txt); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents,0,fileContents.Length); requestStream.Close(); FtpWebResponse response =(FtpWebResponse)request.GetResponse(); Console.WriteLine(Upload File Complete,status {0},response.StatusDescription); response.Close(); } } } }

但visual studio找不到名称空间 FtpWebRequest 或 WebRequestMethods 。另一种方法,我尝试使用链接但它表示 WinRT信息:'uri':上传内容仅支持'http'和'https'方案。不是ftp。

那么有没有解决方案可以显示进度条和速度?

解决方案

FtpWebRequest类不是UWP应用程序的商店配置文件的一部分,并且不会通过BackgroundTransfer API支持FTP上传(如您确定的那样)。这使您可以通过套接字实现自己的FTP协议。有关说明,请参见 WinRT中的FTP奋进和一个样本的链接。

I want to upload files from windows 10 app using ftp, I tried the following code.

using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso/test.htm"); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso"); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("testfile.txt"); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); } } } }

But visual studio couldn't find the name space FtpWebRequest or WebRequestMethods. The other method i tried using backgroundtransfer api's from link but it says that WinRT information: 'uri': Uploading content is only supported for 'http' and 'https' schemes. not ftp.

So is there any solution to this where i can show progress bar and speed?

解决方案

The FtpWebRequest class is not part of the store profile for UWP apps, and FTP upload (as you determined) is not supported via the BackgroundTransfer API. This leave you with implementing your own FTP protocol over sockets. See FTP Endeavor in WinRT for an explanation and a link to a sample.

更多推荐

C#Ftp在Windows 10通用应用中上传?

本文发布于:2023-11-15 05:25:25,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   Ftp   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!