我尝试使用C#WPF从ftp上传文件 [英] I try file upload from ftp using C# WPF

查看:323
本文介绍了我尝试使用C#WPF从ftp上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System; 
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Net;
使用System.Text;
使用System.Threading.Tasks;使用System.Windows
;使用System.Windows.Controls
;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;使用System.Windows.Navigation
;
使用System.Windows.Shapes;
使用System.IO;
使用System.Net;
名称空间WpfApp1
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}
public static string fileName;
private void Button_Click(object sender,RoutedEventArgs e)
{
{//创建OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



//设置文件扩展名和文件扩展名的过滤器
dlg.DefaultExt =。*;



//通过调用ShowDialog方法显示OpenFileDialog
Nullable< bool> result = dlg.ShowDialog();


//获取所选文件名并显示在TextBox
if(result == true)
{
//打开文档

fileName =(dlg.FileName);


}


//获取用于与服务器通信的对象。
FtpWebRequest request =(FtpWebRequest)WebRequest.Create(ftp://94.73.145.160/Feba_Toplantı);
request.Method = WebRequestMethods.Ftp.UploadFile;

//此示例假定FTP站点使用匿名登录。
request.Credentials = new NetworkCredential(u6341930,TPmp45D2);

//将文件内容复制到请求流。
StreamReader sourceStream = new StreamReader(fileName.ToString());
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

流requestStream = request.GetRequestStream();
requestStream.Write(fileContents,0,fileContents.Length);
requestStream.Close();

FtpWebResponse response =(FtpWebResponse)request.GetResponse();

Console.WriteLine(上传文件完成,状态{0},response.StatusDescription);

response.Close();


}
}
}
}





我尝试了什么:



我正在使用C#WPF我尝试从ftp上传文件但我从这个<$ p收到错误$ p> StreamReader sourceStream = new StreamReader(fileName.ToString());

错误是550没有找到文件我怎样才能解决我的问题请帮帮忙

解决方案

这个第三方库应该让它更容易: GitHub - robinrodricks / FluentFTP:.NET和FTP的FTP和FTPS客户端.NET Standard,针对速度进行了优化。提供广泛的FTP命令,文件上传/下载,SSL / TLS连接,自动目录列表解析,文件哈希/校验和,文件权限/ CHMOD,FTP代理,UTF-8支持,异步/等待支持等。完全用C#编写,没有外部依赖。 [ ^ ] - 为您提供大量示例和文档。 :)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net;
namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string fileName;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            {  // Create OpenFileDialog 
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



                // Set filter for file extension and default file extension 
                dlg.DefaultExt = ".*";



                // Display OpenFileDialog by calling ShowDialog method 
                Nullable<bool> result = dlg.ShowDialog();


                // Get the selected file name and display in a TextBox 
                if (result == true)
                {
                    // Open document 

                    fileName = (dlg.FileName);


                }


                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://94.73.145.160/Feba_Toplantı");
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("u6341930", "TPmp45D2");

                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(fileName.ToString());
                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();


            }
        }
    }
}



What I have tried:

I am using C# WPF I try to upload file from ftp but I got error from this

StreamReader sourceStream = new StreamReader(fileName.ToString());

error is 550 no find file how can I fix my problem please help guys

解决方案

This 3rd-party library should make it a lot easier for you: GitHub - robinrodricks/FluentFTP: An FTP and FTPS client for .NET & .NET Standard, optimized for speed. Provides extensive FTP commands, File uploads/downloads, SSL/TLS connections, Automatic directory listing parsing, File hashing/checksums, File permissions/CHMOD, FTP proxies, UTF-8 support, Async/await support and more. Written entirely in C#, with no external dependencies.[^] - lots of examples and documentation for you. :)


这篇关于我尝试使用C#WPF从ftp上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆