使用ftp上传文件 [英] Upload file using ftp

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

问题描述

嗨;

我在IIS服务器上使用网站面板。

我想使用ftp上传文件



我使用此代码:

Hi;
I am using Website panel on an IIS server .
I want to upload a file using ftp

I used this code :

try{
       WebClient webClient = new WebClient();
       OpenFileDialog fd = new OpenFileDialog();
       fd.ShowDialog();
       MessageBox.Show(fd.FileName);
       webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

      }
   catch (Exception ex)
   { MessageBox.Show(ex.Message);


   }





此代码运行无误。当我使用网站面板的文件管理器时,一个文件创建了Name.ext,但是这个文件大小是0 KB!为什么?!我的问题是什么?

我确定Windows防火墙允许我这样做。



This code runs without error . when I use file manager of website panel , A file created "Name.ext" but this file size is 0 KB !!! Why ?! What is my problem ?!
I am sure that windows firewall allow me to do it.

推荐答案

首先,你需要检查<$返回的结果c $ c> fd.ShowDialog(); 它可能会被取消。此外,您还执行异步操作。在实际上传文件时,调用不会阻塞调用线程。相反,上传刚刚开始,将在稍后完成。您可以检查文件是否实际上是稍后上传的。



另外,要同步异步操作,您应该使用 WebClient.UploadFileTaskAsync 返回的结果:

http://msdn.microsoft.com/ en-us / library / hh194308%28v = vs.110%29.aspx [ ^ ]。



了解它和<$ c的用法$ c>任务< T> 使用异步操作,您可能需要学习使用一般任务。



参见: http://msdn.microsoft.com/en-us/library/dd321424% 28v = vs.110%29.aspx [ ^ ],

HTTP: //msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dd460717(v = vs.110)的.aspx [ ^ ]。



-SA
First, you need to check up the result returned by fd.ShowDialog(); it might be cancelled. Also, you do the asynchronous operation. The call is not blocking the calling thread by the time the file is actually uploaded. Instead, the upload just starts and will be finished at some later time. You can check up if the file is actually uploaded later.

Also, to synchronize the asynchronous operation, you should use the returned result from WebClient.UploadFileTaskAsync:
http://msdn.microsoft.com/en-us/library/hh194308%28v=vs.110%29.aspx[^].

To understand it and the usage of Task<T> with asynchronous operations, you may need to learn working with Tasks in general.

See also: http://msdn.microsoft.com/en-us/library/dd321424%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx[^].

—SA


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

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